0

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?

2
  • 2
    Your script is not necessarily a 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.
    – doneal24
    Commented Nov 13, 2022 at 17:47
  • Understood. Please post this into an answer so I can accept it.
    – Siderius
    Commented Nov 13, 2022 at 17:54

1 Answer 1

2

Your script is not necessarily a 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .