Skip to content

Linux Command Interpreter

Every script that will be executed on Linux must indicate the Command Interpreter, it must start with the characters “#! ” followed by the full path of the command interpreter. These characters are known as “shebang”.

Example of a shell script called bomdia:

#! /bin/bash

clear
echo “Hello $USER”
echo “Today is “; date
echo “Number of connected users:”; who | wc -l
echo “Calendar”
cal
exit 0 As previously studied, for a file to be considered executable, its permission must be changed and the executable bit enabled:

$ chmod +x good morning

This way the short script can be executed at the prompt:

Hello, Uiraribeiro
Today is
Fri Sep 27 10:56:54 -03 2019
Number of connected users:
2
Calendar
September 2019
Do you have to have to know
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 analyzes the contents of the line “#!” and loads the defined program 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 is in a waiting state.

The new process started reruns the /etc/bashrc and ~/.bashrc files.

An important aspect is that the parent shell (where the script was called) can pass variables to the child shell process that will actually execute the script. But the child shell won’t be able to pass variables or change their content to the parent process. Inheritance is a one-way path from parent process to child process.

It is important that the call on the line “#!” which defines which interpreter should be executed is correct. If the path is incorrect, the script may not run. Comments in Bash scripts, on the other hand, are flagged with just a “#” clue. Everything else of the line will be ignored by the shell.

Linux Script Execution Permissions

In order for a script to be executed, it must be allowed to execute and the chmod command can be used to enable this bit for a script.

$ chmod +x script

A script will always run with the permissions of the user who executed it, and in no way will the Kernel obey the SUID or SGID bit for scripting.

However, the script may be executed with the permissions of another user if the “sudo” command is used.

Take the example of this script that prints the user’s UID:

#! /bin/bash

echo “I am:”
id If executed normally:

$. /myscript

I am:
uid=1000 (uira) gid=1000 (uira) groups=1000 (uira) ,4 (adm) ,10 (wheel) ,190 (systemd-journal) But if executed with sudo:

$ I sweat. /myscript

I am:
uid=0 (root) gid=0 (root) groups=0 (root) Therefore, extreme care must be taken when running a script with “sudo “, since all commands that the script invokes will be executed with the permissions of another user, 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 register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share