If you’re not a programmer, you may feel apprehensive about programming. But shell scripting (or programming) in Linux can be as simple as storing a few commands in a file. In fact, you can have a useful shell program that has a single command.

Shell scripts are popular among system administrators. If you are a system administrator, you can build a collection of custom shell scripts that help you automate tasks you perform often. If a hard drive seems to be getting full, for example, you may want to find all files that exceed some size (say, 1MB) and that have not been accessed in the past 30 days.

In addition, you may want to send an e-mail message to all users who have large files, requesting that they archive and clean up those files. You can perform all these tasks with a shell script. You might start with the following find command to identify large files:

find / -type f -atime +30 -size +1000k -exec ls -l {} \; > /tmp/largefiles

This command creates a file named /tmp/largefiles, which contains detailed information about old files taking up too much space. After you get a list of the files, you can use a few other Linux commands — such as sort, cut, and sed — to prepare and send mail messages to users who have large files to clean up.

Instead of typing all these commands manually, place them in a file and create a shell script. That, in a nutshell, is the essence of shell scripts — to gather shell commands in a file so that you can easily perform repetitive system administration tasks.

bash scripts, just like most Linux commands, accept command-line options. Inside the script, you can refer to the options as $1, $2, and so on. The special name $0 refers to the name of the script itself.

Here’s a typical bash script that accepts arguments:

echo "This script's name is: $0"
echo Argument 1: $1
echo Argument 2: $2

The first line runs the /bin/sh program, which subsequently processes the rest of the lines in the script. The name /bin/sh traditionally refers to the Bourne shell — the first Unix shell. In most Linux systems, /bin/sh is a symbolic link to /bin/bash, which is the executable program for bash.

Save this simple script in a file named simple and make that file executable with the following command:

chmod +x simple

Now run the script as follows:


It displays the following output:

This script's name is: ./simple
Argument 1:
Argument 2:

The first line shows the script’s name. Because you have run the script without arguments, the script displays no values for the arguments.

Now try running the script with a few arguments, like this:

./simple "This is one argument" second-argument third

This time the script displays more output:

This script's name is: ./simple
Argument 1: This is one argument
Argument 2: second-argument

As the output shows, the shell treats the entire string within the double quotation marks as a single argument. Otherwise the shell uses spaces as separators between arguments on the command line.

This sample script ignores the third argument because the script is designed to print only the first two arguments.