Note:to facilitate faster loading of EdCert documents, this page was borrowed from another source.
A shell script is the UNIX equivalent of the VM/CMS EXEC file or the VMS DCL procedure although you should be aware that individual shell commands have limits in the size of their argument strings.
The UNIX shell can be used as an interpretive programming language. Within the shell you can, besides executing shell commands, create and use variables; process (read) arguments; test, branch, and loop; do I/O; etc.
A shell script is a file containing a sequence of commands which can be executed by the shell. The easiest way to execute a script is by typing in the filename on the command line. The shell then interprets and executes the commands in the file one by one.
Although you can write complex programs using the shell language, you can also use simple shell scripts for running complex commands or a series of commands that you use frequently.
The most commonly-used shells in our environment are the C shell and the Bourne shell, or derivatives of these. Many people believe that although the C shell is better for interactive use, the Bourne shell is better for scripts, so you may see many scripts written in the Bourne shell.
The default shell for scripts is the Bourne shell and C shell scripts must begin with a comment line (a line which begins with the character #).
Note that in order to execute a script, the file containing it must have execute permission and the shell may need to rebuild its table of commands.
chmod a+x mycommand
It is important to remember that, like all UNIX commands that are not built-in to the shell, a script file executes in a child shell forked by the parent shell. The shell running the script file retains environment variables of the parent shell as well as those variables defined in the shell startup file for that shell (e.g. .cshrc for the C shell) which is executed before the script. However, at the end of the script, control returns to the parent script and any definitions made by the child process are not passed back to the parent process. If you want to execute commands that affect the current shell, you must use the source command (C shell):
source script
(For the Bourne shell, use the . command instead.)
Refer to a good UNIX reference manual to learn about shell programming; see the Bibliography at the end of this Guide for references.