John John - 5 months ago 8
Linux Question

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

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

Answer

Your first line is wrong:

# !/bin/zsh

It should be:

#!/bin/zsh
Comments