The Bash shell is your main port of entry to Linux, since the shell interprets everything you enter on the command line before passing it along to the operating system for execution.
A shell script is a program written for Bash. Basically script is a sequence of Linux commands, but when you add the power of variables and data manipulating, you can do a lot more with it.
Shell scripts are interpreted, which means that the shell reads each line and acts on it immediately. This process differs from that of a formal programming language like C or C++, where the program is compiled and optimized for faster execution. So there’s a tradeoff–it’s easier to create quick little shell scripts, but if the task at hand requires serious number crunching or complicated logic, a compiled language is better.
Note: All of the shell script syntax and examples in this section will work in both the Bash and Korn (pdksh) shells. The C shell (tcsh) has subtle differences in many areas, so scripts written for one shell may not work in another. If you decide to use the C shell instead of Bash, use the man tcsh command for more information on writing shell scripts for that environment.
Creating a Shell Script
To create a shell script, use a text editor and enter your Linux commands as if you were typing them at the command prompt. For example, try this:
echo “Removing temp files…”
If you save those four lines in a file named deltemp, you will have a simple shell script that automates the process of switching to the /tmp directory, listing all the files there, and deleting the ones that start with the word junk.
So how do you run this little wonder of technology? In DOS, all you have to do is name a file with a .bat extension and it’ll be recognized as an executable file–but not so with Linux. Since Linux attaches no meaning to file extensions, you have to mark the file as executable by using the chmod command, like this:
$ chmod +x deltemp
The x marks the file as executable; if you list the permissions for the deltemp file afterward, you will see the x in position four, confirming this:
$ ls -l deltemp
If you want other users to be able to run this script, give them both read and execute permission, like so:
$ chmod ugo+rx deltemp
$ ls -l deltemp
Now the permissions show that any user can view or execute the deltemp script, but only you can modify it. To run the script, just enter its name at the command prompt, prefixed with ./ as shown here:
Note: If the current directory is in the PATH environment variable, you can omit the ./ before the name.
But there’s one important thing you should know about running shell scripts this way. When you enter the shell script name and tell Bash to run it, a subshell is created for the execution of the script. This subshell inherits all the shell environment variables, including the current directory, but you lose any changes made to that environment when the script terminates.
What’s the practical meaning of this? Well, you might expect that the current directory would be /tmp after you’ve run the deltemp script, but it’s not. You’ll still be in hermie’s home directory. And if we had set an environment variable inside the script, its value would be good only during the execution of the script. Here’s an example to demonstrate this point. Create an executable setvar script with these lines:
PS1=’My Prompt: ‘
Now watch how the values of the current directory and the PS1 variable change:
$ PS1=’Bash Me! ‘
$ echo $PS1
Bash Me! PS1 before setvar.
My Prompt: PS1 during setvar.
$ echo $PS1
Bash Me! PS1 after setvar.
It looks like this script is absolutely useless for the intended purpose of setting the environment variable. But a little trick will make it work the way you want. Run the script by prefixing the name with a dot and a space, like so:
This tells Bash to run the setvar script in the current shell environment, instead of creating a temporary subshell. Verify for yourself that the command prompt does in fact change to My Prompt: after running this script