I have started working on bash scripts. I have just observed a difference between executing a command line and executing the same commands but inside a script. In particular the command line is:
for a in {2..10..2};do echo "My number is:$a";done
which produces the following and expected output:
My number is:2
My number is:4
My number is:6
My number is:8
My number is:10
On the other hand, the script test.sh
is the following:
#!/bin/sh
for a in {2..10..2};do echo "My number is:$a";done
For this reason when I do:
./test.sh
I get the following output:
My number is:{2..10..2}
I would have expected these two approaches to be equivalent.
Why are these two approaches different and how can I obtain a proper use of the loop/curly bracket in order to get the same output as the command line?
bash
script since/bin/sh
is linked to different shells in different distributions. It it is linked to/bin/bash
, then all bash extensions to POSIX are disabled. Change your shebang to/bin/bash
to get the behavior you want.