Scripts

A shell script is a series of shell commands, similar to the ones given above, contained in a file, which can be given to the bash program to be executed in sequence. Technically, it is a program with "bash" as the language.

You can use any text editor to create your shell script - simply choose one you are comfortable with. If you are using KDE you can use the Advanced Editor (kate or kwrite), or kedit. Do not use a word processing program - these are not text editors, and insert additional formatting information into the text. If, however, you know how to use your word processor to save plain-text file, go ahead and use it.

Start with a new file, and type the following lines.

#!/bin/bash
echo "I am about to list the home directory"
# here is the actual listing:
ls $HOME
echo "Done!"

The first line has a special meaning. It tells the current shell which program should be used to interpret this file. Here, we have given the filename of the bash program. This is so that, if this script is invoked from within a different shell, or from a file-management program such as Konqueror and Nautilus, it will still know that this script requires the bash shell to run it.

The second line is our first command. We use the "echo" command to display a simple line of information.

The third line is a comment. Bash simply ignores it, but it can be useful to a person reading the program in the future to understand what the code is trying to do. While comments are not very useful in such a short, simple script, they are vital in longer and more cryptic ones.

This is followed by two more commands - the "ls" command which takes the directory name as a parameter, and finally another "echo" to display our final declaration of triumph!

When you save this file, it shows up as an ordinary file which cannot be executed. In order to run it, you must make it executable. To do this, you use the "chmod" command. In the following example, I have used "myscript" as the script name - you should change that to whichever name you used.

chmod u+x myscript

That’s it! You can now execute the new script. To do this, enter the folloowing command, again replacing "myscript" with the name you used if different.

sandbox@laptop:~ >./myscript
I am about to list the home directory
KDesktop  myscript  public_html  snapshot1.png  usr-listing.txt
Done!

Note the "./" at the beginning of the file - this is actually a directory name (a single period means the current directory). You can avoid this by placing the new script, and any others you create, in a directory within the PATH environment variable. Then you can enter the command from wherever you are.

Bash’s startup scripts

Bash uses a special, hidden file in your home directory called ".bashrc". This is essentially the same as any other script, except that it lacks the first line we used above, and it is executed automatically by bash as soon as it starts up. This is where you should place any modified environment variables or other settings for them to be "remembered" by bash every time you log in.