Use of shebang #!/bin/bash in Linux shell scripts.

#! is known as shebang sequence.

It marks the start of every script in linux.

It tells the system what interpreter to use when executing the particular code written in the file.

The #! characters also referred as a magic number. We use this magic number in our scripts under UNIX / Linux operating systems to inform kernel which interpreter to use in order to execute and read our script.

How to use the shebang sequence ?

[root@ngelinux testdir]# cat script1.sh 
#!/bin/bash
echo "Welcome to NGELinux"
echo "-------------------"

[root@ngelinux testdir]# chmod +x script1.sh 
[root@ngelinux testdir]# ./script1.sh 
Welcome to NGELinux
-------------------
[root@ngelinux testdir]# 

 

Issue: Suppose bash doesn’t exists inside /bin.
In this case, we can use env command which takes the path from shell path variable.

[root@ngelinux testdir]# cat script2.sh 
#!/usr/bin/env bash
echo "Welcome to NGELinux"
echo "-------------------"

[root@ngelinux testdir]# ./script2.sh 
Welcome to NGELinux
-------------------
[root@ngelinux testdir]#

Leave a Reply

Your email address will not be published.