Shell Scripting-Shebang (#!)

LINUX_www.basisguru.com

The #! syntax is used in shell scripts to indicate an interpreter for execution under UNIX / Linux operating systems.

Let us create a small test script:

The #! syntax  is used in shell scripts to indicate an interpreter for execution under UNIX / Linux operating systems.
shebang

make sure it is executable:

Here is what happened when we execute the script:

[[email protected]]# ./testscript.sh 
This is my first script
[[email protected]]# 

you will notice that the first line of the script starts with #! and next to it is the path to bash shell program. (/bin/bash)
This term #! is called as ‘Sharp Bang‘ or Shebang.

When any scripts first line starts with Shebang,whatever follwed by it is used as an interpretor for the command listed in the script.
For e.g

#!/bin/csh
echo "this test script used csh as an interpreter.
#!/bin/ksh
echo "this test script used ksh as an interpreter.

So basically when we execute a script which contains shebang, interpreter is executed and the path used to call the script is passed as an argument to the interpreter.

If we do not provide shebang specifically, commands in the script will be executed using the current shell.