What is the bash she bang? How does it work? Why we need to use she bang?
In the executable scripts, the system expects an executable line called shebang, that indicates which program to runs with interpreter. Simple words what program we run the file. The Bash Shell, Python, PHP or others.
Ubuntu find bash in /bin directory:
ubuntu:~/Bash_Scripts# which bash
/usr/bin/bash
ubuntu:~/Bash_Scripts# which -a bash
/usr/bin/bash
/bin/bash
Same inode number then there is same file:
ubuntu:~/Bash_Scripts# ll -i /usr/bin/bash /bin/bash
489 -rwxr-xr-x 1 root root 1183448 Apr 18 2022 /bin/bash*
489 -rwxr-xr-x 1 root root 1183448 Apr 18 2022 /usr/bin/bash*
Adding #!/bin/bash path to the script, /bin or /usr bash are the same file:
ubuntu:~/Bash_Scripts# vim fist_script.sh
1 #!/bin/bash
2 mkdir day01
3 echo "first script text" > day01/text.txt
4 ls -l day01
5 cat day01/text.txt
ubuntu:~/Bash_Scripts# ./first_script.sh
mkdir: cannot create directory 'day01': File exists
total 4
-rw-r - r - 1 root root 18 Feb 28 14:40 text.txt
first script text
Let's create a simple python script, then run it:
ubuntu:~/Bash_Scripts# vim test_py
1 import sys
2 print(sys.version)
ubuntu:~/Bash_Scripts# vim test_py
ubuntu:~/Bash_Scripts# chmod 700 test_py && ./test_py
./test_py: line 1: import: command not found
./test_py: line 2: syntax error near unexpected token `sys.version'
./test_py: line 2: `print(sys.version)'
This error is about bash, we need to add path for the Python interpreter:
ubuntu:~/Bash_Scripts# vim test_py
1 #!/bin/python3
2 import sys
3 print(sys.version)
ubuntu:~/Bash_Scripts# ./test_py
3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0]
Conclusion
If we execute without she bang, it will use your default shell as an interpreter. #!/bin… Is not a comment this is the shebang by the way make your code clean and readable also use comments. Note: set nu to ~.vimrc helps to see numbered lines with vim.