Difference between source, zsh, "." and directly shell script calling

309 Views Asked by At

I have very simple script(for_loop4.sh) as shown below.

# !/bin/zsh
#

for (( i = 0; i < 3; i++ ))
do
    echo $i;
done

I'm using zsh 5.0.7 for my shell.

When running this script using source, zsh and . command, it works as expected. But, when calling this script directly, it fails with the message "Bad for loop variable".

Why this happens ? What's difference ?

$ source ./for_loop4.sh
0
1
2

$ zsh ./for_loop4.sh
0
1
2

$ . ./for_loop4.sh
0
1
2

$ ./for_loop4.sh
./for_loop4.sh: 4: ./for_loop4.sh: Syntax error: Bad for loop variable
2

There are 2 best solutions below

2
On BEST ANSWER

Your first line is wrong:

# !/bin/zsh

It should be:

#!/bin/zsh
0
On

Make sure that where is your zsh installed. It can be installed into /usr/ directory.
You can type,

$ which zsh