In This Chapter
An Introduction to Bash Scripting
Scripting versus Interactive Shell Use
User Variables and Environmental Variables
Arithmetic in Bash
Tests, Conditionals, and Loops in Bash
Script Input and Output
Dissecting of Useful Scripts
In this chapter, we ll explore script programming in the Bourne-Again SHell, otherwise known as bash. Bash is the de facto standard command shell and shell scripting language on Linux and other UNIX systems. We ll investigate some of the major language features of bash, including variables, arithmetic, control structures, input and output, and function creation. This follows the flow of all other scripting chapters in this book, allowing the reader to easily understand the similarities and differences of each covered language.
Before jumping in to scripting with bash, let s look at some preliminaries that are necessary to run bash scripts. You can tell which shell you re currently using by interrogating the SHELL environment variable:
$ echo $SHELL /bin/bash $
The echo command is used to print to the screen. We print the contents of the SHELL variable, accessing the variable by preceding it with the $ symbol. The result is the shell that we re currently operating on, in this case, bash. Technically, it printed out the location of the shell we re using (the bash interpreter is found within the /bin subdirectory).
If we happened not to be using bash, we could simply invoke bash to start that interpreter:
$ echo $SHELL /bin/csh $ bash $
In this case, we were using another command shell (C-shell here). We invoke bash to start this interpreter for further commanding .
Let s now write a simple script as a first step to bash scripting. The source for the script is shown in Listing 20.1.
#!/bin/bash echo "Welcome to $SHELL scripting." exit
When invoked, this script simply emits the line Welcome to /bin/bash scripting. and then exits. If you tried to enter this script (named first.sh ) and execute it as ./first.sh , you d notice that it didn t work. You probably saw something like this:
$ ./first.sh -bash: ./first.sh: Permission denied. $
The problem here is that the script is not executable. We must first change the attributes of the file to tell GNU/Linux that it can be executed. This is done using the chmod command, as illustrated below:
$ chmod +x first.sh $ ./first.sh Welcome to /bin/bash scripting. $
We use chmod to set the execute attribute of the file first.sh , telling GNU/Linux that it can be executed. After trying to execute again, we see the expected results.
The question we could ask ourselves now is, even though we ve told GNU/_Linux that the file can be executed, how does it know that the file is a bash script? The answer is the shebang line in our script. You ll notice that the first line of the script starts with #! (also called shabang ) followed by the path and interpreter ( /bin/bash ). This defines that bash is the shell to be used to interpret this file. If the file had contained a Perl script, it would have begun #! /bin/perl . We can also add comments to our script simply by preceding them with a # character.
Since our interpreter is also our shell, we can perform this command interactively, such as:
$ echo "Welcome to $SHELL scripting." Welcome to /bin/bash scripting. $
Now that we have some basics under our belt, let s dig into bash and work through some more useful scripts.