Home » Linux Basics » 04 - Environment, Configuration, and Programming
4

Linux Shell Scripts

Performing a set of OS tasks through a shell script

Files like .bashrc and so on are called shell scripts. A shell script is a series of Linux programs that perform a set of OS tasks. The permission of a shell script should be set to 'execute' to allow users to run it on the command line. Here is a basic example of a shell script. This script merely prints a "Current Date" label and displays the current date on the next line:

    [LinuxUser:Sun Nov 26] ~$ cat myScript
#!/bin/bash
echo Current Date:
date
#Done

The first line indicates that the script should be executed by the bash shell. This liner is important; users may choose to use a variety of shells; the korn shell will not understand a script written for the bash shell and so on. Explicitly specifying the shell in the first line causes Linux to use the specified shell to execute the program. The next two lines are simple Linux commands that display a label and the current date. The last line is a comment; comments start with the '#' symbol and may be on any line. The first line is interpreted specially.

Let us suppose that the above lines are saved into a file called 'myScript'. The following example demonstrates how the permissions for the file should be set to enable script execution. You may avoid using the current path indicator './' by placing the script in a directory that is part of the 'PATH' variable.

    [ LinuxUser ] ~$ chmod a+x myScript
[ LinuxUser ] ~$ ./myScript
Current Date:
Sun Nov 26 01:40:07 IST 2006
[ LinuxUser ] ~$