Harshith's Site

Sourcing vs Running Shell Script

Jan 6, 2022

Suppose we have a script like this

echo "hello"

saved as hello.sh and is made executable with chmod u+x hello.sh

Now what is the difference between the following commands.

./hello.sh   # A
sh hello.sh  # B
. hello.sh   # C

All of them produce the same output


In command A, the script is executed as an executable, so the system looks at the first line of the script for the shebang #!/bin/sh and executes the script with sh. In B the executable sh is directly told to run the file. So here A and B do the same thing. ( Notice that you don’t need the script to be executable for the later )

To Be continued…