0

I wrote this shellscript file. But I get error near unexpected tokendone'`

#!/bin/bash
i=1
while [ $i -lt 12 ]; do
    echo Hi
    i=$[$i+1]
done

Previously, there was no ; before do. I read stackexchange answers and wrote ; after while [], still I get error. I could not found the resolution online. Any ideas?

1
  • Are you sure your script is run by bash? If you are executing it with sh myscript, and sh isn't a link to bash, your script is being run by some other shell (probably dash) which doesn't support bash's ancient $[...] syntax. Commented Nov 26, 2014 at 18:29

4 Answers 4

1

i=$[$i+1] this is wrong. You probably meant i=$((i+1))

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

2 Comments

It's not so much wrong as antiquated and undocumented. $[...] was the bash notation for arithmetic expressions before POSIX $((...)) was adopted.
ah, nice, I didn't know
0

path to bash is incorrect, you have missed a root in path change first line to

#!/bin/bash

1 Comment

fixed that. But still I have problem
0

Correct placement of ; in a bash script can be tricky indeed. Here's a one-liner version of your script with semicolons in the right place:

i=1; while (( i < 12 )); do echo "Hi"; (( i++ )); done

Note that the $ in variables becomes redundant for integer comparisons if placed between double parentheses (( )).

Comments

0

Works perfectly

$ ./ttt
Hi
Hi
Hi
Hi 
Hi
Hi
Hi
Hi
Hi
Hi 
Hi
$ cat ttt
#!/bin/bash
i=1
while [ $i -lt 12 ]; do
    echo Hi
    i=$[$i+1]
done  
$

Comments

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.