Shebang (#!) in Linux: Choosing the Shell Script Interpreter
Every script that will be executed on Linux must indicate the command interpreter, and on its first line it must start with the characters "#!" followed by the full path of the command interpreter. These characters are known as the "shebang".
Example of a shell script called bomdia:
#!/bin/bashclearecho “Olá $USER”echo “Hoje é “;dateecho “Número de usuários conectados: ” ; who | wc -lecho “Calendário”calexit 0
As studied previously, for a file to be considered executable, its permission must be changed and the executable bit enabled:
$ chmod +x bomdia
This way, the small script can be executed at the prompt:
Olá uiraribeiro
Hoje é
sex set 27 10:56:54 -03 2019
Número de usuários conectados:
2
Calendário
setembro 2019
do se te qu qu se sá
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Other interpreters can be invoked by the "#!" line, such as /bin/sh, /usr/bin/perl, /usr/bin/awk, among others.
When a script is executed, the shell parses the content of the "#!" line and loads the program defined to interpret the script.
In a shell script, this line causes a new shell program to be executed to interpret the script. Meanwhile, the shell that executed it remains in a waiting state.
The new process started re-executes the files /etc/bashrc and ~/.bashrc.
An important aspect is that the parent shell (in which the script was called) can pass variables to the child shell process that will actually execute the script. But the child shell cannot pass variables or change their content back to the parent process. Inheritance is a one-way path from parent process to child process.
It is important that the call in the "#!" line that defines which interpreter should be executed is correct. If the path is incorrect, the script may not be executed. Comments in Bash scripts are marked with just a hash "#". The rest of the line will be ignored by the shell.
Script Execution Permissions on Linux
For a script to be executed, it needs execution permission and the chmod command can be used to enable this bit for a script.
$ chmod +x script
A script will always be executed with the permissions of the user who executed it, and the Kernel will not obey the SUID or SGID bit for scripts in any way.
However, the script can be executed with another user's permissions if the "sudo" command is used.
See this example script that prints the user's UID:
#!/bin/bashecho "eu sou:"id
If executed normally:
$ ./meuscripteu sou:uid=1000(uira) gid=1000(uira) grupos=1000(uira),4(adm),10(wheel),190(systemd-journal)
But if executed with sudo:
$ sudo ./meuscripteu sou:uid=0(root) gid=0(root) grupos=0(root)
Therefore, one must take extreme care when executing a script with "sudo", since all commands invoked by the script will be executed with another user's permissions, and possibly root.
The relative path "./" was used in the example to call the script, because the current directory is not listed in the PATH variable.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.