Confusing syntax error near unexpected token 'done'
No, shell scripts are not tab sensitive (unless you do something really crazy, which you are not doing in this example).
You can't have an empty while do done
block, (comments don't count)Try substituting echo $name
instead
#!/bin/bashnames=(test test2 test3 test4)for name in ${names[@]}do printf "%s " $namedoneprintf "\n"
output
test test2 test3 test4
dash
and bash
are a bit brain-dead in this case, they do not allow an empty loop so you need to add a no op command to make this run, e.g. true
or :
. My tests suggest the :
is a bit faster, although they should be the same, not sure why:
time (i=100000; while ((i--)); do :; done)
n average takes 0.262
seconds, while:
time (i=100000; while ((i--)); do true; done)
takes 0.293
seconds. Interestingly:
time (i=100000; while ((i--)); do builtin true; done)
takes 0.356
seconds.
All measurements are an average of 30 runs.
Bash has a built-in no-op, the colon (:), which is more lightweightthan spawning another process to run true
.
#!/bin/bashnames=(test test2 test3 test4)for name in "${names[@]}"do :done
EDIT: William correctly points out that true
is also a shell built-in, so take this answer as just another option FYI, not a better solution than using true.