A Shell program file (also known as shell script) is a simple file containing text. The shell will interpret this line by line. Every line will be a different command, just as you would have entered one command after another on the command line of a terminal. The shell will not go ahead until one of the lines is completed, if you do not specifically tell it to do so.
The syntax of a command line, as we learned in a previous article titled The Terminal in UNIX, obeys the following sequence:
[Command name] [Options] [Arguments]
The comment character is the # (hash mark). Nothing after this character in the current line will be interpreted. For example:
#Here we will sort the file alfa.txt
sort alfa.txt
The first line of a shell script is special. Here we can specify with what interpreter we would like to execute the script. This happens by starting the first line with a comment and an exclamation mark followed by the interpreter we would like to use. For example, let us write a simple script that will display the output "Live with Passion." We will create a file with the name LWP.sh and enter within it the following lines:
#!/bin/bash
echo Live with Passion
There should be no space between the exclamation mark and the full path to the interpreter. We can run the script in two ways. First, we can just give it to the bash as argument.
bash LWP.sh
When we do, we get this result:
Live With Passion
Another option is to set the execute parameter to on for using chmod. You can read about this in the article titled Rights Management System in UNIX if you missed it. Now we will refer to the file as follows:
/LWP.sh
The output should be the same. The latter example will figure out what interpreter to use with the help of the first line. Therefore, the execution is in fact the creation of a new shell that will get as input the new file. This will link its output to the output of the current shell (terminal), and as you can see, the results. If for some reason the first line is missing, the shell will try to use the default interpreter that lies in the environment variable $SHELL.