1

Here is my simple code:

#!/bin/bash

arr=(3 5 1 2 0 9 8 2)
for i in ${arr[@]}; do printf "%s /n" "$arr[@]" done | sort -n

I am receiving the following error:

syntax error: unexpected end of file

I have a 'for' loop, which I ended with 'done'. What am I missing here?

0

1 Answer 1

3

There are several problems here.

The syntax error is caused by the missing ; before the done. Another syntax error on the same line is the "$arr[@]" instead of "${arr[@]}".

for i in ${arr[@]}; do printf "%s /n" "${arr[@]}"; done | sort -n

Now this is correct syntax, but I don't think it does what you expect.

To print a newline after each number, you need to write \n in the printf instead of /n.

To print the content of the array sorted, you can write:

printf "%s\n" "${arr[@]}" | sort -n

Or write with a loop:

for v in ${arr[@]}; do echo $v; done | sort -n

But using both techniques together doesn't make much sense.

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly well! Thank you sir :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.