The shell script contains commands that appear just as you would enter them on a command line. These commands are stored in a file, but they
appear like regular commands that you would enter manually. For a file to be a shell script, it must have the following items:
The first line of the file must indicate which shell to use for executing the script.
The file permissions must be set to allow the file to be executed as a program
The file must contain valid commands
Defining the execution path
The first line of every shell script indicates the path and filename of the command interpreter, or shell, used to execute the commands in that script.
This line begins with the characters #!. For example, your shell scripts written for the Bourne shell will all begin with the following line:
If you write a shell script for the C shell (which is not covered explicitly in this course), the first line of your script would look like this:
Because the first line of the script indicates which shell will execute the script, you can use one shell (such as the C shell) as your command-line environment, and write scripts that are executed by a different shell. Your command-line shell starts the shell you chose to run the script (which you specified on the first line of the shell script). When the script is finished, you return to your command line shell.
If you want to change to a different shell for your regular command line, ask your system admnistrator to change your user account settings.
You will learn about setting the file permissions for a script file later in this Module.
Shell script file extensions
In many operating systems, file extensions are used to indicate the content or purpose of a file. For example, word processing documents may end with .doc, text files may end with .txt,
and postcript documents may end with .ps.
Even when developing software, files containing source code written in the C language end with .c; the object code compiled from that source code is stored in a file ending with.o.
Shell scripts do not rely on any file extension. You will occasionally see a file with a filename ending in .sh, but this is simply to aid users in recognizing the file as a shell script.
A file can have any filename and still be a shell script, as long as the three conditions listed at the beginning of this lesson are met.
Every shell script must contain commands that would be valid if executed on a command line using the shell you have selected to run the script.
Later modules describe how to use specific commands in your shell scripts.
The following MouseOver applet shows the components of a script that we have discussed in this lesson.