This shell script uncompiled text file that may have an executable that contains commands and system commands to control its execution (instructions, loops, etc.). Is performed only by the interpreter (here /bin/bash), which executes the commands contained in the script (you can say very simply that translates it into the CPU).
Hello world
Any good course begins with the example "Hello World". Not otherwise, will in this case, it will help us to know the structure of a shell script.
We create an empty file
touch helloworld.sh
Then edit it to any text editor (mcedit, pico, nano, vim, emacs, etc.) by typing in it the following contents:
# !/bin/bash
#
# Hello World example
#
echo "Hello world"
#
# Hello World example
#
echo "Hello world"
The first line of script called shebang starting with characters: #! although it looks like a commentary, is not it - is of particular significance - points to the interpreter, which should be used to execute a script, the path to it must be given in absolute form (you can not use the bash), and must be in the first line, without any prior spaces. Here, the script will always be executed by the command interpreter /bin/bash, regardless of what kind of a shell at the moment we use.
The # character indicates a comment, everything is behind him in the same line is ignored by the interpreter.
The only thing we have left is to give the script executable and execute it:
chmod 711 helloworld.sh
./ Helloworld.sh
./ Helloworld.sh
Tip:
You do not have to keep entering the full name of the script - enough to enter a couple of initial letters and press Tab, and Bash will complete the missing letters - if the file exists.
Tip 2:
The first line of the script is not compulsory, most interpreters permits the use of syntax:
/bin/bash script_nam_to_execute
However, I strongly encourage to use the "shebang", as in Unix file extension does not matter (this is not what in Windows).