Advanced Bash Scripting Guide
Advanced Bash Scripting Guide
6.1
30 September 2009
Revision History
Revision 5.6 26 Jan 2009 Revised by: mc
'WORCESTERBERRY' release: Minor Update.
Revision 6.0 23 Mar 2009 Revised by: mc
'THIMBLEBERRY' release: Major Update.
Revision 6.1 30 Sep 2009 Revised by: mc
'BUFFALOBERRY' release: Minor Update.
This tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an
intermediate/advanced level of instruction . . . all the while sneaking in little nuggets of UNIX® wisdom and
lore. It serves as a textbook, a manual for self-study, and a reference and source of knowledge on shell
scripting techniques. The exercises and heavily-commented examples invite active reader participation, under
the premise that the only way to really learn scripting is to write scripts.
This book is suitable for classroom use as a general introduction to programming concepts.
Dedication
For Anita, the source of all the magic
Advanced Bash-Scripting Guide
Table of Contents
Chapter 1. Shell Programming!.........................................................................................................................1
Part 2. Basics.......................................................................................................................................................7
Chapter 5. Quoting...........................................................................................................................................40
5.1. Quoting Variables...........................................................................................................................40
5.2. Escaping..........................................................................................................................................42
Chapter 7. Tests................................................................................................................................................51
7.1. Test Constructs...............................................................................................................................51
7.2. File test operators............................................................................................................................58
7.3. Other Comparison Operators..........................................................................................................61
7.4. Nested if/then Condition Tests.......................................................................................................67
7.5. Testing Your Knowledge of Tests..................................................................................................67
i
Advanced Bash-Scripting Guide
Table of Contents
Chapter 10. Loops and Branches..................................................................................................................134
10.1. Loops..........................................................................................................................................134
10.2. Nested Loops..............................................................................................................................147
10.3. Loop Control...............................................................................................................................148
10.4. Testing and Branching................................................................................................................151
Part 4. Commands..........................................................................................................................................168
ii
Advanced Bash-Scripting Guide
Table of Contents
Chapter 21. Restricted Shells.........................................................................................................................381
iii
Advanced Bash-Scripting Guide
Table of Contents
Chapter 33. Miscellany
33.11. Shell Scripting Under Windows...............................................................................................521
Bibliography....................................................................................................................................................541
Appendix J. Localization................................................................................................................................769
iv
Advanced Bash-Scripting Guide
Table of Contents
Appendix K. History Commands..................................................................................................................773
Appendix N. Exercises....................................................................................................................................792
N.1. Analyzing Scripts.........................................................................................................................792
N.2. Writing Scripts.............................................................................................................................794
Appendix Q. To Do List..................................................................................................................................808
Appendix R. Copyright..................................................................................................................................810
v
Chapter 1. Shell Programming!
No programming language is perfect. There is
not even a single best language; there are only
languages well suited or perhaps poorly suited
for particular purposes.
--Herbert Mayer
A working knowledge of shell scripting is essential to anyone wishing to become reasonably proficient at
system administration, even if they do not anticipate ever having to actually write a script. Consider that as a
Linux machine boots up, it executes the shell scripts in /etc/rc.d to restore the system configuration and
set up services. A detailed understanding of these startup scripts is important for analyzing the behavior of a
system, and possibly modifying it.
The craft of scripting is not hard to master, since the scripts can be built in bite-sized sections and there is only
a fairly small set of shell-specific operators and options [1] to learn. The syntax is simple and straightforward,
similar to that of invoking and chaining together utilities at the command line, and there are only a few "rules"
governing their use. Most short scripts work right the first time, and debugging even the longer ones is
straightforward.
A shell script is a quick-and-dirty method of prototyping a complex application. Getting even a limited subset
of the functionality to work in a script is often a useful first stage in project development. This way, the
structure of the application can be tested and played with, and the major pitfalls found before proceeding to
the final coding in C, C++, Java, Perl, or Python.
Shell scripting hearkens back to the classic UNIX philosophy of breaking complex projects into simpler
subtasks, of chaining together components and utilities. Many consider this a better, or at least more
esthetically pleasing approach to problem solving than using one of the new generation of high powered
all-in-one languages, such as Perl, which attempt to be all things to all people, but at the cost of forcing you to
alter your thinking processes to fit the tool.
According to Herbert Mayer, "a useful language needs arrays, pointers, and a generic mechanism for building
data structures." By these criteria, shell scripting falls somewhat short of being "useful." Or, perhaps not. . . .
• Resource-intensive tasks, especially where speed is a factor (sorting, hashing, recursion [2] ...)
• Procedures involving heavy-duty math operations, especially floating point arithmetic, arbitrary
precision calculations, or complex numbers (use C++ or FORTRAN instead)
• Cross-platform portability required (use C or Java instead)
If any of the above applies, consider a more powerful scripting language -- perhaps Perl, Tcl, Python, Ruby
-- or possibly a compiled language such as C, C++, or Java. Even then, prototyping the application as a
shell script might still be a useful development step.
We will be using Bash, an acronym for "Bourne-Again shell" and a pun on Stephen Bourne's now classic
Bourne shell. Bash has become a de facto standard for shell scripting on most flavors of UNIX. Most of the
principles this book covers apply equally well to scripting with other shells, such as the Korn Shell, from
which Bash derives some of its features, [3] and the C Shell and its variants. (Note that C Shell programming
is not recommended due to certain inherent problems, as pointed out in an October, 1993 Usenet post by Tom
Christiansen.)
What follows is a tutorial on shell scripting. It relies heavily on examples to illustrate various features of the
shell. The example scripts work -- they've been tested, insofar as was possible -- and some of them are even
useful in real life. The reader can play with the actual working code of the examples in the source archive
(scriptname.sh or scriptname.bash), [4] give them execute permission (chmod u+rx
scriptname), then run them to see what happens. Should the source archive not be available, then
cut-and-paste from the HTML or pdf rendered versions. Be aware that some of the scripts presented here
introduce features before they are explained, and this may require the reader to temporarily skip ahead for
enlightenment.
Unless otherwise noted, the author of this book wrote the example scripts that follow.
--Edmund Spenser
--Larry Wall
In the simplest case, a script is nothing more than a list of system commands stored in a file. At the very least,
this saves the effort of retyping that particular sequence of commands each time it is invoked.
# Cleanup
# Run as root, of course.
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up."
There is nothing unusual here, only a set of commands that could just as easily have been invoked one by one
from the command-line on the console or in a terminal window. The advantages of placing the commands in a
script go far beyond not having to retype them time and again. The script becomes a program -- a tool -- and it
can easily be modified or customized for a particular application.
#!/bin/bash
# Proper header for a Bash script.
# Cleanup, version 2
LOG_DIR=/var/log
# Variables are better than hard-coded values.
cd $LOG_DIR
#!/bin/bash
# Cleanup, version 3
# Warning:
# -------
LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=50 # Default number of lines saved.
E_XCD=86 # Can't change directory?
E_NOTROOT=87 # Non-root exit error.
if [ -n "$1" ]
# Test whether command-line argument is present (non-empty).
then
lines=$1
else
lines=$LINES # Default, if not specified on command-line.
fi
cd $LOG_DIR
tail -n $lines messages > mesg.temp # Save last section of message log file.
mv mesg.temp messages # Becomes new log directory.
cat /dev/null > wtmp # ': > wtmp' and '> wtmp' have the same effect.
echo "Logs cleaned up."
exit 0
# A zero return value from the script upon exit indicates success
#+ to the shell.
Since you may not wish to wipe out the entire system log, this version of the script keeps the last section of
the message log intact. You will constantly discover ways of fine-tuning previously written scripts for
increased effectiveness.
***
The sha-bang ( #!) [5] at the head of a script tells your system that this file is a set of commands to be fed to
the command interpreter indicated. The #! is actually a two-byte [6] magic number, a special marker that
designates a file type, or in this case an executable shell script (type man magic for more details on this
fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that
interprets the commands in the script, whether it be a shell, a programming language, or a utility. This
command interpreter then executes the commands in the script, starting at the top (the line following the
sha-bang line), and ignoring comments. [7]
#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed -f
#!/usr/awk -f
Each of the above script header lines calls a different command interpreter, be it /bin/sh, the default shell
(bash in a Linux system) or otherwise. [8] Using #!/bin/sh, the default Bourne shell in most commercial
variants of UNIX, makes the script portable to non-Linux machines, though you sacrifice Bash-specific
features. The script will, however, conform to the POSIX [9] sh standard.
Note that the path given at the "sha-bang" must be correct, otherwise an error message -- usually "Command
not found." -- will be the only result of running the script. [10]
#! can be omitted if the script consists only of a set of generic system commands, using no internal shell
directives. The second example, above, requires the initial #!, since the variable assignment line, lines=50,
uses a shell-specific construct. [11] Note again that #!/bin/sh invokes the default shell interpreter, which
defaults to /bin/bash on a Linux machine.
This tutorial encourages a modular approach to constructing a script. Make note of and collect
"boilerplate" code snippets that might be useful in future scripts. Eventually you will build quite an
extensive library of nifty routines. As an example, the following script prolog tests whether the script has
been invoked with the correct number of parameters.
E_WRONG_ARGS=85
script_parameters="-a -h -m -z"
# -a = all, -h = help, etc.
if [ $# -ne $Number_of_expected_args ]
then
echo "Usage: `basename $0` $script_parameters"
# `basename $0` is the script's filename.
exit $E_WRONG_ARGS
fi
Many times, you will write a script that carries out one particular task. The first script in this chapter is
an example. Later, it might occur to you to generalize the script to do other, similar tasks. Replacing the
literal ("hard-wired") constants by variables is a step in that direction, as is replacing repetitive code
blocks by functions.
Either:
chmod 555 scriptname (gives everyone read/execute permission) [13]
or
chmod +rx scriptname (gives everyone read/execute permission)
chmod u+rx scriptname (gives only the script owner read/execute permission)
Having made the script executable, you may now test it by ./scriptname. [14] If it begins with a
"sha-bang" line, invoking the script calls the correct command interpreter to run it.
As a final step, after testing and debugging, you would likely want to move it to /usr/local/bin (as root,
of course), to make the script available to yourself and all other users as a systemwide executable. The script
could then be invoked by simply typing scriptname [ENTER] from the command-line.
Part 2. Basics 7
Chapter 3. Special Characters
What makes a character special? If it has a meaning beyond its literal meaning, a meta-meaning, then we refer
to it as a special character.
#
Comments. Lines beginning with a # (with the exception of #!) are comments and will not be
executed.
A command may not follow a comment on the same line. There is no method of
terminating the comment, in order for "live code" to begin on the same line. Use a new
line for the next command.
# Thanks, S.C.
The standard quoting and escape characters (" ' \) escape the #.
Certain pattern matching operations also use the #.
;
Command separator [semicolon]. Permits putting two or more commands on the same line.
case "$variable" in
abc) echo "\$variable = abc" ;;
xyz) echo "\$variable = xyz" ;;
esac
;;&, ;&
Terminators in a case option (version 4+ of Bash).
.
"dot" command [period]. Equivalent to source (see Example 14-22). This is a bash builtin.
.
"dot", as a component of a filename. When working with filenames, a leading dot is the prefix of a
"hidden" file, a file that an ls will not normally show.
bash$ ls -al
total 14
drwxrwxr-x 2 bozo bozo 1024 Aug 29 20:54 ./
drwx------ 52 bozo bozo 3072 Aug 29 20:51 ../
-rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.addressbook
-rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.addressbook.bak
-rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.addressbook
-rw-rw-r-- 1 bozo bozo 0 Aug 29 20:54 .hidden-file
When considering directory names, a single dot represents the current working directory, and two dots
denote the parent directory.
bash$ pwd
/home/bozo/projects
bash$ cd .
bash$ pwd
/home/bozo/projects
bash$ cd ..
bash$ pwd
/home/bozo/
The dot often appears as the destination (directory) of a file movement command, in this context
meaning current directory.
# /bin/ipcalc
# /usr/bin/kcalc
# /usr/bin/oidcalc
# /usr/bin/oocalc
\X escapes the character X. This has the effect of "quoting" X, equivalent to 'X'. The \ may be used to
quote " and ', so they are expressed literally.
null command [colon]. This is the shell equivalent of a "NOP" (no op, a do-nothing operation). It
may be considered a synonym for the shell builtin true. The ":" command is itself a Bash builtin, and
its exit status is true (0).
:
echo $? # 0
Endless loop:
while :
do
operation-1
operation-2
...
operation-n
done
# Same as:
# while true
# do
# ...
# done
Placeholder in if/then test:
if condition
then : # Do nothing and branch ahead
else # Or else ...
take-some-action
fi
Provide a placeholder where a binary operation is expected, see Example 8-2 and default parameters.
: ${username=`whoami`}
# ${username=`whoami`} Gives an error without the leading :
# unless "username" is a command or builtin...
Provide a placeholder where a command is expected in a here document. See Example 18-10.
In combination with the > redirection operator, truncates a file to zero length, without changing its
permissions. If the file did not previously exist, creates it.
In combination with the >> redirection operator, has no effect on a pre-existing target file (: >>
target_file). If the file did not previously exist, creates it.
This applies to regular files, not pipes, symlinks, and certain special files.
May be used to begin a comment line, although this is not recommended. Using # for a comment
turns off error checking for the remainder of that line, so almost anything may appear in a comment.
However, this is not the case with :.
In yet another context, from the command line, the ! invokes the Bash history mechanism (see
Appendix K). Note that within a script, the history mechanism is disabled.
*
wild card [asterisk]. The * character serves as a "wild card" for filename expansion in globbing. By
itself, it matches every filename in a given directory.
bash$ echo *
abs-book.sgml add-drive.sh agram.sh alias.sh
The * also represents any number (or zero) characters in a regular expression.
*
arithmetic operator. In the context of arithmetic operations, the * denotes multiplication.
** A double asterisk can represent the exponentiation operator or extended file-match globbing.
?
test operator. Within certain expressions, the ? indicates a test for a condition.
In a double-parentheses construct, the ? can serve as an element of a C-style trinary operator, ?:.
(( var0 = var1<98?9:21 ))
# ^ ^
# if [ "$var1" -lt 98 ]
# then
# var0=9
# else
# var0=21
# fi
In a parameter substitution expression, the ? tests whether a variable has been set.
?
wild card. The ? character serves as a single-character "wild card" for filename expansion in
globbing, as well as representing one character in an extended regular expression.
$
Variable substitution (contents of a variable).
var1=5
var2=23skidoo
echo $var1 # 5
echo $var2 # 23skidoo
Variables inside parentheses, within the subshell, are not visible to the rest of the
script. The parent process, the script, cannot read variables created in the child
process, the subshell.
a=123
( a=321; )
cp file22.{txt,backup}
# Copies "file22.txt" to "file22.backup"
A command may act upon a comma-separated list of file specs within braces. [17] Filename
expansion (globbing) applies to the file specs between the braces.
No spaces allowed within the braces unless the spaces are quoted or escaped.
echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z
# Echoes characters between a and z.
echo {0..3} # 0 1 2 3
# Echoes characters between 0 and 3.
The {a..z} extended brace expansion construction is a feature introduced in version 3 of Bash.
{}
Block of code [curly brackets]. Also referred to as an inline group, this construct, in effect, creates
an anonymous function (a function without a name). However, unlike in a "standard" function, the
variables inside a code block remain visible to the remainder of the script.
bash$ { local a;
a=123; }
bash: local: can only be used in a
function
a=123
{ a=321; }
echo "a = $a" # a = 321 (value inside code block)
# Thanks, S.C.
The code block enclosed in braces may have I/O redirected to and from it.
#!/bin/bash
# Reading lines in /etc/fstab.
File=/etc/fstab
{
read line1
read line2
} < $File
exit 0
#!/bin/bash
# rpm-check.sh
SUCCESS=0
E_NOARGS=65
if [ -z "$1" ]
then
echo "Usage: `basename $0` rpm-file"
exit $E_NOARGS
fi
exit 0
The ";" ends the -exec option of a find command sequence. It needs to be escaped to
protect it from interpretation by the shell.
[]
test.
Test expression between [ ]. Note that [ is part of the shell builtin test (and a synonym for it), not a
link to the external command /usr/bin/test.
[[ ]]
test.
Test expression between [[ ]]. More flexible than the single-bracket [ ] test, this is a shell keyword.
In the context of an array, brackets set off the numbering of each element of that array.
Array[1]=slot_1
echo ${Array[1]}
[]
range of characters.
a=3
b=7
echo $[$a+$b] # 10
echo $[$a*$b] # 21
Note that this usage is deprecated, and has been replaced by the (( ... )) construct.
(( ))
integer expansion.
command &>filename redirects both the stdout and the stderr of command to filename.
This is useful for suppressing output when testing for a condition. For example, let us
test whether a certain command exists.
bash$ echo $?
1
Or in a script:
[i]<>filename opens file filename for reading and writing, and assigns file descriptor i to it. If
filename does not exist, it is created.
process substitution.
(command)>
<(command)
In a different context, the "<" and ">" characters act as string comparison operators.
In yet another context, the "<" and ">" characters act as integer comparison operators. See also
Example 15-9.
<<
redirection used in a here document.
<<<
redirection used in a here string.
<, >
ASCII comparison.
pipe. Passes the output (stdout of a previous command to the input (stdin) of the next one, or to
the shell. This is a method of chaining commands together.
echo ls -l | sh
# Passes the output of "echo ls -l" to the shell,
#+ with the same result as a simple "ls -l".
A pipe, as a classic method of interprocess communication, sends the stdout of one process to the
stdin of another. In a typical case, a command, such as cat or echo, pipes a stream of data to a
filter, a command that transforms its input for processing. [19]
For an interesting note on the complexity of using UNIX pipes, see the UNIX FAQ, Part 3.
The output of a command or commands may be piped to a script.
#!/bin/bash
# uppercase.sh : Changes input to uppercase.
tr 'a-z' 'A-Z'
# Letter ranges must be quoted
#+ to prevent filename generation from single-letter filenames.
exit 0
Now, let us pipe the output of ls -l to this script.
bash$ ls -l | ./uppercase.sh
-RW-RW-R-- 1 BOZO BOZO 109 APR 7 19:49 1.TXT
-RW-RW-R-- 1 BOZO BOZO 109 APR 14 16:48 2.TXT
-RW-R--R-- 1 BOZO BOZO 725 APR 20 20:56 DATA-FILE
The stdout of each process in a pipe must be read as the stdin of the next. If this
is not the case, the data stream will block, and the pipe will not behave as expected.
variable="initial_value"
echo "new_value" | read variable
echo "variable = $variable" # variable = initial_value
If one of the commands in the pipe aborts, this prematurely terminates execution of the
pipe. Called a broken pipe, this condition sends a SIGPIPE signal.
>|
force redirection (even if the noclobber option is set). This will forcibly overwrite an existing file.
||
OR logical operator. In a test construct, the || operator causes a return of 0 (success) if either of the
linked test conditions is true.
&
Run job in background. A command followed by an & will run in the background.
Within a script, commands and even loops may run in the background.
#!/bin/bash
# background-loop.sh
# ======================================================
# Occasionally also:
exit 0
A command run in the background within a script may cause the script to hang,
waiting for a keystroke. Fortunately, there is a remedy for this.
&&
AND logical operator. In a test construct, the && operator causes a return of 0 (success) only if both
the linked test conditions are true.
-
option, prefix. Option flag for a command or filter. Prefix for an operator. Prefix for a default
parameter in parameter substitution.
COMMAND -[Option1][Option2][...]
ls -al
param2=${param1:-$DEFAULTVAL}
# ^
--
sort --ignore-leading-blanks
Used with a Bash builtin, it means the end of options to that particular command.
This provides a handy means of removing files whose names begin with a dash.
bash$ ls -l
-rw-r--r-- 1 bozo bozo 0 Nov 25 12:29 -badname
bash$ rm -- -badname
bash$ ls -l
total 0
The double-dash is also used in conjunction with set.
bash$ cat -
abc
abc
...
Ctl-D
As expected, cat - echoes stdin, in this case keyboarded user input, to stdout. But, does I/O
redirection using - have real-world applications?
# 1) cd /source/directory
# Source directory, where the files to be moved are.
# 2) &&
# "And-list": if the 'cd' operation successful,
# then execute the next command.
# 3) tar cf - .
# The 'c' option 'tar' archiving command creates a new archive,
# the 'f' (file) option, followed by '-' designates the target file
# as stdout, and do it in current directory tree ('.').
# 4) |
# Piped to ...
# 5) ( ... )
# a subshell
# 6) cd /dest/directory
# Change to the destination directory.
# 7) &&
# "And-list", as above
# 8) tar xpvf -
# Unarchive ('x'), preserve ownership and file permissions ('p'),
# and send verbose messages to stdout ('v'),
# reading data from stdin ('f' followed by '-').
#
# Note that 'x' is a command, and 'p', 'v', 'f' are options.
#
# Whew!
bash$ file
Usage: file [-bciknvzL] [-f namefile] [-m magicfiles] file...
Add a "-" for a more useful result. This causes the shell to await user input.
bash$ file -
abc
standard input: ASCII text
bash$ file -
#!/bin/bash
standard input: Bourne-Again shell script text executable
Now the command accepts input from stdin and analyzes it.
The "-" can be used to pipe stdout to other commands. This permits such stunts as prepending lines
to a file.
#!/bin/bash
BACKUPFILE=backup-$(date +%m-%d-%Y)
# Embeds date in backup filename.
# Thanks, Joshua Tschida, for the idea.
archive=${1:-$BACKUPFILE}
# If no backup-archive filename specified on command-line,
#+ it will default to "backup-MM-DD-YYYY.tar.gz."
# Stephane Chazelas points out that the above code will fail
#+ if there are too many files found
#+ or if any filenames contain blank characters.
exit 0
Filenames beginning with "-" may cause problems when coupled with the "-"
redirection operator. A script should check for this and add an appropriate prefix to
such filenames, for example ./-FILENAME, $PWD/-FILENAME, or
$PATHNAME/-FILENAME.
If the value of a variable begins with a -, this may likewise create problems.
var="-n"
echo $var
# Has the effect of "echo -n", and outputs nothing.
-
previous working directory. A cd - command changes to the previous working directory. This uses
the $OLDPWD environmental variable.
Do not confuse the "-" used in this sense with the "-" redirection operator just
discussed. The interpretation of the "-" depends on the context in which it appears.
-
Minus. Minus sign in an arithmetic operation.
=
Equals. Assignment operator
a=28
echo $a # 28
Certain commands and builtins use the + to enable certain options and the - to disable them. In
parameter substitution, the + prefixes an alternate value that a variable expands to.
%
modulo. Modulo (remainder of a division) arithmetic operation.
bash$ echo ~
/home/bozo
bash$ echo ~/
/home/bozo/
bash$ echo ~:
/home/bozo:
~+
current working directory. This corresponds to the $PWD internal variable.
~-
previous working directory. This corresponds to the $OLDPWD internal variable.
=~
regular expression match. This operator was introduced with version 3 of Bash.
^
beginning-of-line. In a regular expression, a "^" addresses the beginning of a line of text.
^, ^^
Uppercase conversion in parameter substitution (added in version 4 of Bash).
Control Characters
change the behavior of the terminal or text display. A control character is a CONTROL + key
combination (pressed simultaneously). A control character may also be written in octal or
hexadecimal notation, following an escape.
◊ Ctl-A
Backspace (nondestructive).
◊
Ctl-C
When typing text on the console or in an xterm window, Ctl-D erases the character under
the cursor. When there are no characters present, Ctl-D logs out of the session, as expected.
In an xterm window, this has the effect of closing the window.
◊ Ctl-E
BEL. On some old-time teletype terminals, this would actually ring a bell. In an xterm it
might beep.
◊
Ctl-H
Rubout (destructive backspace). Erases characters the cursor backs over while backspacing.
#!/bin/bash
# Embedding Ctl-H in a string.
Horizontal tab.
◊
Ctl-J
Newline (line feed). In a script, may also be expressed in octal notation -- '\012' or in
hexadecimal -- '\x0a'.
◊ Ctl-K
Vertical tab.
When typing text on the console or in an xterm window, Ctl-K erases from the character
under the cursor to end of line. Within a script, Ctl-K may behave differently, as in Lee Lee
Maschmeyer's example, below.
◊ Ctl-L
Formfeed (clear the terminal screen). In a terminal, this has the same effect as the clear
command. When sent to a printer, a Ctl-L causes an advance to end of the paper sheet.
◊
Ctl-M
Carriage return.
#!/bin/bash
# Thank you, Lee Maschmeyer, for this example.
read -n 1 -s -p \
$'Control-M leaves cursor at beginning of this line. Press Enter. \x0d'
# Of course, '0d' is the hex equivalent of Control-M.
echo >&2 # The '-s' makes anything typed silent,
#+ so it is necessary to go to new line explicitly.
###
exit 0
◊ Ctl-N
Erases a line of text recalled from history buffer [20] (on the command-line).
◊ Ctl-O
Resume (XON).
Suspend (XOFF).
Reverses the position of the character the cursor is on with the previous character (on the
command-line).
◊ Ctl-U
Erase a line of input, from the cursor backward to beginning of line. In some settings, Ctl-U
erases the entire line of input, regardless of cursor position.
◊ Ctl-V
When inputting text, Ctl-V permits inserting control characters. For example, the following
two are equivalent:
echo -e '\x0a'
echo <Ctl-V><Ctl-J>
Ctl-V is primarily useful from within a text editor.
◊ Ctl-W
When typing text on the console or in an xterm window, Ctl-W erases from the character
under the cursor backwards to the first instance of whitespace. In some settings, Ctl-W
erases backwards to first non-alphanumeric character.
◊ Ctl-X
In certain word processing programs, Cuts highlighted text and copies to clipboard.
◊ Ctl-Y
Blank lines have no effect on the action of a script, and are therefore useful for visually separating
functional sections.
$IFS, the special variable separating fields of input to certain commands. It defaults to whitespace.
UNIX filters can target and operate on whitespace using the POSIX character class [:space:].
Variables appear in arithmetic operations and manipulation of quantities, and in string parsing.
Let us carefully distinguish between the name of a variable and its value. If variable1 is the name
of a variable, then $variable1 is a reference to its value, the data item it contains. [22]
bash$ variable1=23
Enclosing a referenced value in double quotes (" ... ") does not interfere with variable substitution.
This is called partial quoting, sometimes referred to as "weak quoting." Using single quotes (' ... ')
causes the variable name to be used literally, and no substitution will take place. This is full quoting,
sometimes referred to as 'strong quoting.' See Chapter 5 for a detailed discussion.
Note that $variable is actually a simplified form of ${variable}. In contexts where the
$variable syntax causes an error, the longer form may work (see Section 9.3, below).
#!/bin/bash
# ex9.sh
a=375
hello=$a
# "VARIABLE =value"
# ^
#% Script tries to run "VARIABLE" command with one argument, "=value".
# "VARIABLE= value"
# ^
#% Script tries to run "value" command with
#+ the environmental variable "VARIABLE" set to "".
#-------------------------------------------------------------------------
# Quoting . . .
echo "$hello" # 375
echo "${hello}" # 375
echo
hello="A B C D"
echo $hello # A B C D
echo "$hello" # A B C D
# As you see, echo $hello and echo "$hello" give different results.
# Why?
# =======================================
# Quoting a variable preserves whitespace.
# =======================================
echo
# --------------------------------------------------------------
# --------------------------------------------------------------
echo; echo
echo; echo
exit 0
An uninitialized variable has a "null" value -- no assigned value at all (not zero!).
if [ -z "$unassigned" ]
then
echo "\$unassigned is NULL."
fi # $unassigned is NULL.
Using a variable before assigning a value to it may cause problems. It is nevertheless
possible to perform arithmetic operations on an uninitialized variable.
# Conclusion:
# An uninitialized variable has no value,
#+ however it acts as if it were 0 in an arithmetic operation.
# This is undocumented (and probably non-portable) behavior,
#+ and should not be used in a script.
See also Example 14-23.
Do not confuse this with = and -eq, which test, rather than assign!
#!/bin/bash
# Naked variables
echo
# Assignment
a=879
echo "The value of \"a\" is $a."
echo
echo
echo
echo
exit 0
#!/bin/bash
exit 0
Variable assignment using the $(...) mechanism (a newer method than backquotes). This is actually a
form of command substitution.
# From /etc/rc.d/rc.local
R=$(cat /etc/redhat-release)
arch=$(uname -m)
Unlike many other programming languages, Bash does not segregate its variables by "type." Essentially, Bash
variables are character strings, but, depending on context, Bash permits arithmetic operations and
comparisons on variables. The determining factor is whether the value of a variable contains only digits.
#!/bin/bash
# int-or-string.sh
a=2334 # Integer.
let "a += 1"
echo "a = $a " # a = 2335
echo # Integer, still.
c=BB34
exit $?
Untyped variables are both a blessing and a curse. They permit more flexibility in scripting and make it easier
to grind out lines of code (and give you enough rope to hang yourself!). However, they likewise permit subtle
errors to creep in and encourage sloppy programming habits.
To lighten the burden of keeping track of variable types in a script, Bash does permit declaring variables.
In a more general context, each process has an "environment", that is, a group of
variables that the process may reference. In this sense, the shell behaves like any other
process.
Every time a shell starts, it creates shell variables that correspond to its own
environmental variables. Updating or adding new environmental variables causes the
shell to update its environment, and all the shell's child processes (the commands it
executes) inherit this environment.
The space allotted to the environment is limited. Creating too many environmental
variables or ones that use up excessive space may cause problems.
bash$ du
bash: /usr/bin/du: Argument list too long
(Thank you, Stéphane Chazelas for the clarification, and for providing the above
example.)
If a script sets environmental variables, they need to be "exported," that is, reported to the
environment local to the script. This is the function of the export command.
A script can export variables only to child processes, that is, only to commands or
processes which that particular script initiates. A script invoked from the
command-line cannot export variables back to the command-line environment.
Child processes cannot export variables back to the parent processes that spawned
them.
$0 is the name of the script itself, $1 is the first argument, $2 the second, $3 the third, and so forth.
[24] After $9, the arguments must be enclosed in brackets, for example, ${10}, ${11}, ${12}.
#!/bin/bash
echo
echo
if [ -n "$2" ]
then
echo "Parameter #2 is $2"
fi
if [ -n "$3" ]
then
echo "Parameter #3 is $3"
fi
# ...
echo "-----------------------------------"
echo "All the command-line parameters are: "$*""
if [ $# -lt "$MINPARAMS" ]
then
echo
echo "This script needs at least $MINPARAMS command-line arguments!"
fi
echo
exit 0
Bracket notation for positional parameters leads to a fairly simple way of referencing the last
argument passed to a script on the command-line. This also requires indirect referencing.
If a script expects a command-line parameter but is invoked without one, this may
cause a null variable assignment, generally an undesirable result. One way to prevent
this is to append an extra character to both sides of the assignment statement using the
expected positional parameter.
variable1_=$1_ # Rather than variable1=$1
# This will prevent an error, even if positional parameter is absent.
critical_argument01=$variable1_
#!/bin/bash
# ex18.sh
E_NOARGS=65
if [ -z "$1" ]
then
echo "Usage: `basename $0` [domain-name]"
exit $E_NOARGS
fi
exit $?
---
The shift command reassigns the positional parameters, in effect shifting them to the left one notch.
The old $1 disappears, but $0 (the script name) does not change. If you use a large number of
positional parameters to a script, shift lets you access those past 10, although {bracket} notation also
permits this.
#!/bin/bash
# shft.sh: Using 'shift' to step through all the positional parameters.
exit
#!/bin/bash
# shift-past.sh
echo "$1"
exit 0
# ======================== #
$ sh shift-past.sh 1 2 3 4 5
4
The shift command works in a similar fashion on parameters passed to a function. See
Example 33-16.
Quoting means just that, bracketing a string in quotes. This has the effect of protecting special characters in
the string from reinterpretation or expansion by the shell or shell script. (A character is "special" if it has an
interpretation other than its literal meaning. For example, the asterisk * represents a wild card character in
globbing and Regular Expressions).
bash$ ls -l [Vv]*
-rw-rw-r-- 1 bozo bozo 324 Apr 2 15:05 VIEWDATA.BAT
-rw-rw-r-- 1 bozo bozo 507 May 4 14:25 vartrace.sh
-rw-rw-r-- 1 bozo bozo 539 Apr 14 17:11 viewdata.sh
bash$ ls -l '[Vv]*'
ls: [Vv]*: No such file or directory
In everyday speech or writing, when we "quote" a phrase, we set it apart and give it special meaning. In a
Bash script, when we quote a string, we set it apart and protect its literal meaning.
Certain programs and utilities reinterpret or expand special characters in a quoted string. An important use of
quoting is protecting a command-line parameter from the shell, but still letting the calling program expand it.
Use double quotes to prevent word splitting. [27] An argument enclosed in double quotes presents itself as a
single word, even if it contains whitespace separators.
Chapter 5. Quoting 40
Advanced Bash-Scripting Guide
echo "---"
variable2="" # Empty.
Enclosing the arguments to an echo statement in double quotes is necessary only when word splitting or
preservation of whitespace is an issue.
#!/bin/bash
# weirdvars.sh: Echoing weird variables.
echo
var="'(]\\{}\$\""
echo $var # '(]\{}$"
echo "$var" # '(]\{}$" Doesn't make a difference.
echo
IFS='\'
echo $var # '(] {}$" \ converted to space. Why?
echo "$var" # '(]\{}$"
Chapter 5. Quoting 41
Advanced Bash-Scripting Guide
echo
var2="\\\\\""
echo $var2 # "
echo "$var2" # \\"
echo
# But ... var2="\\\\"" is illegal. Why?
var3='\\\\'
echo "$var3" # \\\\
# Strong quoting works, though.
exit
Single quotes (' ') operate similarly to double quotes, but do not permit referencing variables, since the special
meaning of $ is turned off. Within single quotes, every special character except ' gets interpreted literally.
Consider single quotes ("full quoting") to be a stricter method of quoting than double quotes ("partial
quoting").
Since even the escape character (\) gets a literal interpretation within single quotes, trying to enclose a
single quote within single quotes will not yield the expected result.
echo
5.2. Escaping
Escaping is a method of quoting single characters. The escape (\) preceding a character tells the shell to
interpret that character literally.
With certain commands and utilities, such as echo and sed, escaping a character may have the opposite
effect - it can toggle on a special meaning for that character.
Chapter 5. Quoting 42
Advanced Bash-Scripting Guide
#!/bin/bash
# escaped.sh: escaped characters
echo; echo
# Escaping a newline.
# ------------------
echo ""
echo; echo
echo "============="
echo "==============="
echo "QUOTATION MARKS"
# Version 2 and later of Bash permits using the $'\nnn' construct.
# Note that in this case, '\nnn' is an octal value.
echo $'\t \042 \t' # Quote (") framed by tabs.
Chapter 5. Quoting 43
Advanced Bash-Scripting Guide
echo
echo
echo; echo
echo; echo
exit 0
See Example 34-1 for another example of the $' ... ' string-expansion construct.
\"
gives the quote its literal meaning
# Whereas . . .
# However . . .
Chapter 5. Quoting 44
Advanced Bash-Scripting Guide
# Command substitution
echo `echo \z` # z
echo `echo \\z` # z
echo `echo \\\z` # \z
echo `echo \\\\z` # \z
echo `echo \\\\\\z` # \z
echo `echo \\\\\\\z` # \\z
echo `echo "\z"` # \z
echo `echo "\\z"` # \z
# Here document
cat <<EOF
\z
EOF # \z
cat <<EOF
\\z
EOF # \z
variable=\
echo "$variable"
# Will not work - gives an error message:
# test.sh: : command not found
# A "naked" escape cannot safely be assigned to a variable.
#
# What actually happens here is that the "\" escapes the newline and
#+ the effect is variable=echo "$variable"
#+ invalid variable assignment
variable=\
23skidoo
echo "$variable" # 23skidoo
# This works, since the second line
#+ is a valid variable assignment.
variable=\
# \^ escape followed by space
echo "$variable" # space
variable=\\
echo "$variable" # \
variable=\\\
echo "$variable"
# Will not work - gives an error message:
# test.sh: \: command not found
#
# First escape escapes second one, but the third one is left "naked",
#+ with same result as first instance, above.
Chapter 5. Quoting 45
Advanced Bash-Scripting Guide
variable=\\\\
echo "$variable" # \\
# Second and fourth escapes escaped.
# This is o.k.
Escaping a space can prevent word splitting in a command's argument list.
echo "-------------------------------------------------------------------------"
The escape also provides a means of writing a multi-line command. Normally, each separate line constitutes a
different command, but an escape at the end of a line escapes the newline character, and the command
sequence continues on to the next line.
# As an alternative:
tar cf - -C /source/directory . |
tar xpvf - -C /dest/directory
# See note below.
# (Thanks, Stéphane Chazelas.)
If a script line ends with a |, a pipe character, then a \, an escape, is not strictly necessary. It is, however,
good programming practice to always escape the end of a line of code that continues to the following
line.
echo "foo
bar"
#foo
#bar
echo
echo 'foo
bar' # No difference yet.
#foo
#bar
echo
echo foo\
bar # Newline escaped.
#foobar
echo
Chapter 5. Quoting 46
Advanced Bash-Scripting Guide
echo "foo\
bar" # Same here, as \ still interpreted as escape within weak quotes.
#foobar
echo
echo 'foo\
bar' # Escape character \ taken literally because of strong quoting.
#foo\
#bar
Chapter 5. Quoting 47
Chapter 6. Exit and Exit Status
... there are dark corners in the Bourne shell, and
people use all of them.
--Chet Ramey
The exit command terminates a script, just as in a C program. It can also return a value, which is available to
the script's parent process.
Every command returns an exit status (sometimes referred to as a return status or exit code). A successful
command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an
error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful
completion, though there are some exceptions.
Likewise, functions within a script and the script itself return an exit status. The last command executed in the
function or script determines the exit status. Within a script, an exit nnn command may be used to deliver
an nnn exit status to the shell (nnn must be an integer in the 0 - 255 range).
When a script ends with an exit that has no parameter, the exit status of the script is the exit status of the
last command executed in the script (previous to the exit).
#!/bin/bash
COMMAND_1
. . .
COMMAND_LAST
exit
The equivalent of a bare exit is exit $? or even just omitting the exit.
#!/bin/bash
COMMAND_1
. . .
COMMAND_LAST
exit $?
#!/bin/bash
COMMAND1
. . .
COMMAND_LAST
$? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the
last command executed in the function. This is Bash's way of giving functions a "return value." [28]
Following the execution of a pipe, a $? gives the exit status of the last command executed.
After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last
command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on
error.
#!/bin/bash
echo hello
echo $? # Exit status 0 returned because command executed successfully.
echo
The !, the logical not qualifier, reverses the outcome of a test or command, and this affects its exit status.
! true
echo "exit status of \"! true\" = $?" # 1
# Note that the "!" needs a space between it and the command.
# !true leads to a "command not found" error
#
# The '!' operator prefixing a command invokes the Bash history mechanism.
true
!true
# No error this time, but no negation either.
# It just repeats the previous command (true).
# =========================================================== #
# Preceding a _pipe_ with ! inverts the exit status returned.
ls | bogus_command # bash: bogus_command: command not found
echo $? # 127
echo $? # 0
# Note that the ! does not change the execution of the pipe.
# Only the exit status changes.
# =========================================================== #
Certain exit status codes have reserved meanings and should not be user-specified in a script.
Every reasonably complete programming language can test for a condition, then act according to the result of
the test. Bash has the test command, various bracket and parenthesis operators, and the if/then construct.
• An if/then construct tests whether the exit status of a list of commands is 0 (since 0 means "success"
by UNIX convention), and if so, executes one or more commands.
• There exists a dedicated command called [ (left bracket special character). It is a synonym for test,
and a builtin for efficiency reasons. This command considers its arguments as comparison expressions
or file tests and returns an exit status corresponding to the result of the comparison (0 for true, 1 for
false).
• With version 2.02, Bash introduced the [[ ... ]] extended test command, which performs comparisons
in a manner more familiar to programmers from other languages. Note that [[ is a keyword, not a
command.
(( 200 || 11 )) # Logical OR
echo $? # 0 ***
# ...
let "num = (( 200 || 11 ))"
echo $num # 1
let "num = (( 200 || 11 ))"
echo $? # 0 ***
(( 200 | 11 )) # Bitwise OR
echo $? # 0 ***
# ...
let "num = (( 200 | 11 ))"
echo $num # 203
let "num = (( 200 | 11 ))"
echo $? # 0 ***
Chapter 7. Tests 51
Advanced Bash-Scripting Guide
• An if can test any command, not just conditions enclosed within brackets.
word=Linux
letter_sequence=inu
if echo "$word" | grep -q "$letter_sequence"
# The "-q" option to grep suppresses output.
then
echo "$letter_sequence found in $word"
else
echo "$letter_sequence not found in $word"
fi
if COMMAND_WHOSE_EXIT_STATUS_IS_0_UNLESS_ERROR_OCCURRED
then echo "Command succeeded."
else echo "Command failed."
fi
• These last two examples courtesy of Stéphane Chazelas.
#!/bin/bash
# Tip:
# If you're unsure of how a certain condition would evaluate,
#+ test it in an if-test.
echo
echo
echo
Chapter 7. Tests 52
Advanced Bash-Scripting Guide
echo
echo
echo
echo
echo
Chapter 7. Tests 53
Advanced Bash-Scripting Guide
echo
echo
echo
exit 0
Exercise. Explain the behavior of Example 7-1, above.
if [ condition-true ]
then
command 1
command 2
...
else # Or else ...
# Adds default code block executing if original condition tests false.
command 3
command 4
...
fi
When if and then are on same line in a condition test, a semicolon must terminate the if statement. Both if
and then are keywords. Keywords (or commands) begin statements, and before a new statement on the
same line begins, the old one must terminate.
if [ -x "$filename" ]; then
elif
elif is a contraction for else if. The effect is to nest an inner if/then construct within an outer one.
Chapter 7. Tests 54
Advanced Bash-Scripting Guide
if [ condition1 ]
then
command1
command2
command3
elif [ condition2 ]
# Same as else if
then
command4
command5
else
default-command
fi
The test command is a Bash builtin which tests file types and compares strings. Therefore, in a Bash
script, test does not call the external /usr/bin/test binary, which is part of the sh-utils package.
Likewise, [ does not call /usr/bin/[, which is linked to /usr/bin/test.
If, for some reason, you wish to use /usr/bin/test in a Bash script, then specify it by full
pathname.
#!/bin/bash
echo
if test -z "$1"
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
Chapter 7. Tests 55
Advanced Bash-Scripting Guide
echo "First command-line argument is $1."
fi
echo
echo
echo
exit 0
The [[ ]] construct is the more versatile Bash version of [ ]. This is the extended test command, adopted from
ksh88.
***
No filename expansion or word splitting takes place between [[ and ]], but there is parameter expansion and
command substitution.
file=/etc/passwd
if [[ -e $file ]]
then
echo "Password file exists."
fi
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the
&&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
Arithmetic evaluation of octal / hexadecimal constants takes place automatically within a [[ ... ]] construct.
decimal=15
octal=017 # = 15 (decimal)
Chapter 7. Tests 56
Advanced Bash-Scripting Guide
hex=0x0f # = 15 (decimal)
Following an if, neither the test command nor the test brackets ( [ ] or [[ ]] ) are strictly necessary.
dir=/home/bozo
Similarly, a condition within test brackets may stand alone without an if, when used in combination with
a list construct.
var1=20
var2=22
[ "$var1" -ne "$var2" ] && echo "$var1 is not equal to $var2"
home=/home/bozo
[ -d "$home" ] || echo "$home directory does not exist."
The (( )) construct expands and evaluates an arithmetic expression. If the expression evaluates as zero, it
returns an exit status of 1, or "false". A non-zero expression returns an exit status of 0, or "true". This is in
marked contrast to using the test and [ ] constructs previously discussed.
#!/bin/bash
# Arithmetic tests.
Chapter 7. Tests 57
Advanced Bash-Scripting Guide
(( 0 ))
echo "Exit status of \"(( 0 ))\" is $?." # 1
(( 1 ))
echo "Exit status of \"(( 1 ))\" is $?." # 0
(( 5 > 4 )) # true
echo "Exit status of \"(( 5 > 4 ))\" is $?." # 0
(( 5 > 9 )) # false
echo "Exit status of \"(( 5 > 9 ))\" is $?." # 1
(( 5 - 5 )) # 0
echo "Exit status of \"(( 5 - 5 ))\" is $?." # 1
(( 5 / 4 )) # Division o.k.
echo "Exit status of \"(( 5 / 4 ))\" is $?." # 0
# ======================================= #
var1=5
var2=4
exit 0
-e
file exists
-a
file exists
This is identical in effect to -e. It has been "deprecated," [30] and its use is discouraged.
-f
file is a regular file (not a directory or device file)
-s
file is not zero size
Chapter 7. Tests 58
Advanced Bash-Scripting Guide
-d
file is a directory
-b
file is a block device
-c
file is a character device
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a
given script is a terminal.
-r
file has read permission (for the user running the test)
-w
file has write permission (for the user running the test)
-x
file has execute permission (for the user running the test)
-g
set-group-id (sgid) flag set on file or directory
If a directory has the sgid flag set, then a file created within that directory belongs to the group that
owns the directory, not necessarily to the group of the user who created the file. This may be useful
for a directory shared by a workgroup.
-u
Chapter 7. Tests 59
Advanced Bash-Scripting Guide
A binary owned by root with set-user-id flag set runs with root privileges, even when an
ordinary user invokes it. [31] This is useful for executables (such as pppd and cdrecord) that need to
access system hardware. Lacking the suid flag, these binaries could not be invoked by a non-root
user.
Commonly known as the sticky bit, the save-text-mode flag is a special type of file permission. If a
file has this flag set, that file will be kept in cache memory, for quicker access. [32] If set on a
directory, it restricts write permission. Setting the sticky bit adds a t to the permissions on the file or
directory listing.
If a user does not own a directory that has the sticky bit set, but has write permission in that directory,
she can only delete those files that she owns in it. This keeps users from inadvertently overwriting or
deleting each other's files in a publicly accessible directory, such as /tmp. (The owner of the
directory or root can, of course, delete or rename files there.)
-O
you are owner of file
-G
group-id of file same as yours
-N
file modified since it was last read
f1 -nt f2
file f1 is newer than f2
f1 -ot f2
file f1 is older than f2
f1 -ef f2
files f1 and f2 are hard links to the same file
!
"not" -- reverses the sense of the tests above (returns true if condition absent).
#!/bin/bash
# broken-link.sh
# Written by Lee bigelow <[email protected]>
# Used in ABS Guide with permission.
# A pure shell script to find dead symlinks and output them quoted
#+ so they can be fed to xargs and dealt with :)
#+ eg. sh broken-link.sh /somedir /someotherdir|xargs rm
#
# This, however, is a better method:
#
# find "somedir" -type l -print0|\
# xargs -r0 file|\
Chapter 7. Tests 60
Advanced Bash-Scripting Guide
# grep "broken symbolic"|
# sed -e 's/^\|: *broken symbolic.*$/"/g'
#
#+ but that wouldn't be pure Bash, now would it.
# Caution: beware the /proc file system and any circular links!
################################################################
linkchk () {
for element in $1/*; do
[ -h "$element" -a ! -e "$element" ] && echo \"$element\"
[ -d "$element" ] && linkchk $element
# Of course, '-h' tests for symbolic link, '-d' for directory.
done
}
# Send each arg that was passed to the script to the linkchk() function
#+ if it is a valid directoy. If not, then print the error message
#+ and usage info.
##################
for directory in $directorys; do
if [ -d $directory ]
then linkchk $directory
else
echo "$directory is not a directory"
echo "Usage: $0 dir1 dir2 ..."
fi
done
exit $?
Example 28-1, Example 10-7, Example 10-3, Example 28-3, and Example A-1 also illustrate uses of the file
test operators.
integer comparison
-eq
is equal to
Chapter 7. Tests 61
Advanced Bash-Scripting Guide
-ne
is not equal to
string comparison
is equal to
if [ "$a" = "$b" ]
==
is equal to
if [ "$a" == "$b" ]
Chapter 7. Tests 62
Advanced Bash-Scripting Guide
The == comparison operator behaves differently within a double-brackets test than within
single brackets.
if [ "$a" != "$b" ]
if [ -z "$String" ]
then
echo "\$String is null."
else
echo "\$String is NOT null."
fi # $String is null.
-n
string is not null.
The -n test requires that the string be quoted within the test brackets. Using an
unquoted string with ! -z, or even just the unquoted string alone within test brackets
(see Example 7-6) normally works, however, this is an unsafe practice. Always quote a
tested string. [33]
Chapter 7. Tests 63
Advanced Bash-Scripting Guide
#!/bin/bash
a=4
b=5
echo
echo
if [ "$a" != "$b" ]
then
echo "$a is not equal to $b."
echo "(string comparison)"
# "4" != "5"
# ASCII 52 != ASCII 53
fi
echo
exit 0
#!/bin/bash
# str-test.sh: Testing null strings and unquoted strings,
#+ but not strings and sealing wax, not to mention cabbages and kings . . .
# Using if [ ... ]
echo
Chapter 7. Tests 64
Advanced Bash-Scripting Guide
echo
echo
string1=initialized
string1="a = b"
#!/bin/bash
# zmore
E_NOARGS=65
E_NOTFOUND=66
E_NOTGZIP=67
Chapter 7. Tests 65
Advanced Bash-Scripting Guide
# $1 can exist, but be empty: zmore "" arg2 arg3
then
echo "Usage: `basename $0` filename" >&2
# Error message to stderr.
exit $E_NOARGS
# Returns 65 as exit status of script (error code).
fi
filename=$1
if [ ${filename##*.} != "gz" ]
# Using bracket in variable substitution.
then
echo "File $1 is not a gzipped file!"
exit $E_NOTGZIP
fi
zcat $1 | more
compound comparison
-a
logical and
exp1 -a exp2 returns true if both exp1 and exp2 are true.
-o
logical or
These are similar to the Bash comparison operators && and ||, used within double brackets.
if [ "$expr1" -a "$expr2" ]
then
echo "Both expr1 and expr2 are true."
else
echo "Either expr1 or expr2 is false."
fi
Chapter 7. Tests 66
Advanced Bash-Scripting Guide
# However ...
[ 1 -eq 2 -a -n "`echo true 1>&2`" ] # true
# ^^^^^^^ False condition. So, why "true" output?
a=3
if [ "$a" -gt 0 ]
then
if [ "$a" -lt 5 ]
then
echo "The value of \"a\" lies somewhere between 0 and 5."
fi
fi
if [ -f $HOME/.Xclients ]; then
exec $HOME/.Xclients
elif [ -f /etc/X11/xinit/Xclients ]; then
exec /etc/X11/xinit/Xclients
else
# failsafe settings. Although we should never get here
# (we provide fallbacks in Xclients as well) it can't hurt.
xclock -geometry 100x100-5+5 &
xterm -geometry 80x50-50+150 &
if [ -f /usr/bin/netscape -a -f /usr/share/doc/HTML/index.html ]; then
netscape /usr/share/doc/HTML/index.html &
fi
fi
Chapter 7. Tests 67
Advanced Bash-Scripting Guide
Explain the test constructs in the above snippet, then examine an updated version of the file,
/etc/X11/xinit/xinitrc, and analyze the if/then test constructs there. You may need to refer ahead to
the discussions of grep, sed, and regular expressions.
Chapter 7. Tests 68
Chapter 8. Operations and Related Topics
8.1. Operators
assignment
variable assignment
Initializing or changing the value of a variable
=
All-purpose assignment operator, which works for both arithmetic and string assignments.
var=27
category=minerals # No spaces allowed after the "=".
Do not confuse the "=" assignment operator with the = test operator.
# = as a test operator
if [ "$string1" = "$string2" ]
then
command
fi
arithmetic operators
+
plus
-
minus
*
multiplication
/
division
**
exponentiation
let "z=5**3" # 5 * 5 * 5
echo "z = $z" # z = 125
%
modulo, or mod (returns the remainder of an integer division operation)
bash$ expr 5 % 3
2
This operator finds use in, among other things, generating numbers within a specific range (see
Example 9-26 and Example 9-30) and formatting program output (see Example 26-16 and Example
A-6). It can even be used to generate prime numbers, (see Example A-15). Modulo turns up
surprisingly often in numerical recipes.
#!/bin/bash
# gcd.sh: greatest common divisor
# Uses Euclid's algorithm
# ------------------------------------------------------
# Argument check
ARGS=2
E_BADARGS=85
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` first-number second-number"
exit $E_BADARGS
fi
# ------------------------------------------------------
gcd ()
{
gcd $1 $2
# Exercises :
# ---------
# 1) Check command-line arguments to make sure they are integers,
#+ and exit the script with an appropriate error message if not.
# 2) Rewrite the gcd () function to use local variables.
exit 0
+=
plus-equal (increment variable by a constant)
#!/bin/bash
# Counting to 11 in 10 different ways.
: $((n = $n + 1))
# ":" necessary because otherwise Bash attempts
#+ to interpret "$((n = $n + 1))" as a command.
echo -n "$n "
(( n = n + 1 ))
# A simpler alternative to the method above.
# Thanks, David Lombard, for pointing this out.
echo -n "$n "
n=$(($n + 1))
echo -n "$n "
: $[ n = $n + 1 ]
# ":" necessary because otherwise Bash attempts
#+ to interpret "$[ n = $n + 1 ]" as a command.
# Works even if "n" was initialized as a string.
n=$[ $n + 1 ]
# Works even if "n" was initialized as a string.
#* Avoid this type of construct, since it is obsolete and nonportable.
# Thanks, Stephane Chazelas.
echo -n "$n "
echo
exit 0
Integer variables in older versions of Bash were signed long (32-bit) integers, in the range of
-2147483648 to 2147483647. An operation that took a variable outside these limits gave an erroneous
result.
a=2147483646
echo "a = $a" # a = 2147483646
let "a+=1" # Increment "a".
echo "a = $a" # a = 2147483647
let "a+=1" # increment "a" again, past the limit.
echo "a = $a" # a = -2147483648
# ERROR: out of range,
# + and the leftmost bit, the sign bit,
# + has been set, making the result negative.
As of version >= 2.05b, Bash supports 64-bit integers.
Bash does not understand floating point arithmetic. It treats numbers containing a decimal point as
strings.
a=1.5
bitwise operators. The bitwise operators seldom make an appearance in shell scripts. Their chief use seems to
be manipulating and testing values read from ports or sockets. "Bit flipping" is more relevant to compiled
languages, such as C and C++, which provide direct access to system hardware.
bitwise operators
<<
bitwise left shift (multiplies by 2 for each shift position)
<<=
left-shift-equal
!
NOT
if [ ! -f $FILENAME ]
then
...
&&
AND
&& may also be used, depending on context, in an and list to concatenate commands.
||
OR
if [ $condition1 ] || [ $condition2 ]
# Same as: if [ $condition1 -o $condition2 ]
# Returns true if either condition1 or condition2 holds true...
Bash tests the exit status of each statement linked with a logical operator.
#!/bin/bash
a=24
b=47
a=rhino
b=crocodile
if [ "$a" = rhino ] && [ "$b" = crocodile ]
then
echo "Test #5 succeeds."
else
echo "Test #5 fails."
fi
exit 0
The && and || operators also find use in an arithmetic context.
bash$ echo $(( 1 && 2 )) $((3 && 0)) $((4 || 0)) $((0 || 0))
1 0 1 0
miscellaneous operators
,
Comma operator
The comma operator chains together two or more arithmetic operations. All the operations are
evaluated (with possible side effects. [34]
#!/bin/bash
# numbers.sh: Representation of numbers in different bases.
echo
# Important note:
# --------------
# Using a digit out of range of the specified base notation
#+ gives an error message.
$ sh numbers.sh
$ echo $?
$ 1
bash4$ echo $$
11015
But ...
#!/bin/bash4
echo
for n in 0 1 2 3 4 5
do
echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"
done
Checking $BASH_VERSION is a good method of determining which shell is running. $SHELL does
not necessarily give the correct answer.
$CDPATH
A colon-separated list of search paths available to the cd command, similar in function to the $PATH
variable for binaries. The $CDPATH variable may be set in the local ~/.bashrc file.
bash$ cd bash-doc
bash: cd: bash-doc: No such file or directory
bash$ CDPATH=/usr/share/doc
bash$ cd bash-doc
/usr/share/doc/bash-doc
$DIRSTACK
The top value in the directory stack [35] (affected by pushd and popd)
This builtin variable corresponds to the dirs command, however dirs shows the entire contents of the
directory stack.
$EDITOR
The default editor invoked by a script, usually vi or emacs.
$EUID
"effective" user ID number
Identification number of whatever identity the current user has assumed, perhaps by means of su.
$FUNCNAME
Name of the current function
xyz23 ()
{
echo "$FUNCNAME now executing." # xyz23 now executing.
}
xyz23
This is a listing (array) of the group id numbers for current user, as recorded in /etc/passwd and
/etc/group.
$HOME
Home directory of the user, usually /home/username (see Example 9-16)
$HOSTNAME
The hostname command assigns the system host name at bootup in an init script. However, the
gethostname() function sets the Bash internal variable $HOSTNAME. See also Example 9-16.
$HOSTTYPE
host type
This variable determines how Bash recognizes fields, or word boundaries, when it interprets character
strings.
$IFS defaults to whitespace (space, tab, and newline), but may be changed, for example, to parse a
comma-separated data file. Note that $* uses the first character held in $IFS. See Example 5-1.
$IFS does not handle whitespace the same as it does other characters.
#!/bin/bash
# ifs.sh
output_args_one_per_line()
{
for arg
do
echo "[$arg]"
done # ^ ^ Embed within brackets, for your viewing pleasure.
}
IFS=" "
var=" a b c "
# ^ ^^ ^^^
output_args_one_per_line $var # output_args_one_per_line `echo " a b c "`
# [a]
# [b]
# [c]
IFS=:
var=":a::b:c:::" # Same pattern as above,
# ^ ^^ ^^^ #+ but substituting ":" for " " ...
output_args_one_per_line $var
# []
# [a]
# []
# [b]
# [c]
# []
# []
echo
exit
(Many thanks, Stéphane Chazelas, for clarification and above examples.)
See also Example 15-41, Example 10-7, and Example 18-14 for instructive examples of using $IFS.
$IGNOREEOF
Ignore EOF: how many end-of-files (control-D) the shell will ignore before logging out.
$LC_COLLATE
Often set in the .bashrc or /etc/profile files, this variable controls collation order in filename
expansion and pattern matching. If mishandled, LC_COLLATE can cause unexpected results in
filename globbing.
When given a command, the shell automatically does a hash table search on the directories listed in
the path for the executable. The path is stored in the environmental variable, $PATH, a list of
directories, separated by colons. Normally, the system stores the $PATH definition in
/etc/profile and/or ~/.bashrc (see Appendix G).
The current "working directory", ./, is usually omitted from the $PATH as a security
measure.
$PIPESTATUS
Array variable holding exit status(es) of last executed foreground pipe.
The members of the $PIPESTATUS array hold the exit status of each respective command executed
in a pipe. $PIPESTATUS[0] holds the exit status of the first command in the pipe,
$PIPESTATUS[1] the exit status of the second command, and so on.
The $PIPESTATUS variable may contain an erroneous 0 value in a login shell (in
releases prior to 3.0 of Bash).
tcsh% bash
The above lines contained in a script would produce the expected 0 1 0 output.
Thank you, Wayne Pollock for pointing this out and supplying the above example.
bash$ $ ls | bogus_command | wc
Chet Ramey attributes the above output to the behavior of ls. If ls writes to a pipe
whose output is not read, then SIGPIPE kills it, and its exit status is 141. Otherwise
its exit status is 0, as expected. This likewise is the case for tr.
bash$ $ ls | bogus_command | wc
bash: bogus_command: command not found
0 0 0
The pipefail option may be useful in cases where $PIPESTATUS does not give the
desired information.
$PPID
The $PPID of a process is the process ID (pid) of its parent process. [36]
#!/bin/bash
E_WRONG_DIRECTORY=83
cd $TargetDirectory
echo "Deleting stale files in $TargetDirectory."
if [ "$PWD" != "$TargetDirectory" ]
then # Keep from wiping out wrong directory by accident.
echo "Wrong directory!"
echo "In $PWD, rather than $TargetDirectory!"
echo "Bailing out!"
exit $E_WRONG_DIRECTORY
fi
rm -rf *
rm .[A-Za-z0-9]* # Delete dotfiles.
# rm -f .[^.]* ..?* to remove filenames beginning with multiple dots.
# (shopt -s dotglob; rm -f *) will also work.
# Thanks, S.C. for pointing this out.
echo
echo "Done."
echo "Old files deleted in $TargetDirectory."
echo
exit $?
$REPLY
The default value when a variable is not supplied to read. Also applicable to select menus, but only
supplies the item number of the variable chosen, not the value of the variable itself.
#!/bin/bash
# reply.sh
echo
echo -n "What is your favorite vegetable? "
read
echo
echo -n "What is your favorite fruit? "
read fruit
echo "Your favorite fruit is $fruit."
echo "but..."
echo "Value of \$REPLY is still $REPLY."
# $REPLY is still set to its previous value because
#+ the variable $fruit absorbed the new "read" value.
echo
exit 0
$SECONDS
The number of seconds the script has been running.
#!/bin/bash
TIME_LIMIT=10
INTERVAL=1
echo
echo "Hit Control-C to exit before $TIME_LIMIT seconds."
echo
exit 0
$SHELLOPTS
The list of enabled shell options, a readonly variable.
$SHLVL
Shell level, how deeply Bash is nested. [37] If, at the command-line, $SHLVL is 1, then in a script it
will increment to 2.
This variable is not affected by subshells. Use $BASH_SUBSHELL when you need
an indication of subshell nesting.
$TMOUT
If the $TMOUT environmental variable is set to a non-zero value time, then the shell prompt will
time out after $time seconds. This will cause a logout.
As of version 2.05b of Bash, it is now possible to use $TMOUT in a script in combination with read.
if [ -z "$song" ]
There are other, more complex, ways of implementing timed input in a script. One alternative is to set
up a timing loop to signal the script when it times out. This also requires a signal handling routine to
trap (see Example 29-5) the interrupt generated by the timing loop (whew!).
#!/bin/bash
# timed-input.sh
TIMER_INTERRUPT=14
TIMELIMIT=3 # Three seconds in this instance.
# May be set to different value.
PrintAnswer()
{
if [ "$answer" = TIMEOUT ]
then
echo $answer
else # Don't want to mix up the two instances.
echo "Your favorite veggie is $answer"
kill $! # Kills no-longer-needed TimerOn function
#+ running in background.
# $! is PID of last job running in background.
fi
TimerOn()
{
sleep $TIMELIMIT && kill -s 14 $$ &
# Waits 3 seconds, then sends sigalarm to script.
}
Int14Vector()
{
answer="TIMEOUT"
PrintAnswer
exit $TIMER_INTERRUPT
}
exit 0
#!/bin/bash
# timeout.sh
timedout_read() {
timeout=$1
varname=$2
old_tty_settings=`stty -g`
stty -icanon min 0 time ${timeout}0
eval read $varname # or just read $varname
stty "$old_tty_settings"
# See man page for "stty."
}
echo
echo
exit 0
Perhaps the simplest method is using the -t option to read.
#!/bin/bash
# t-out.sh
# Inspired by a suggestion from "syngin seven" (thanks).
TIMELIMIT=4 # 4 seconds
echo
if [ -z "$variable" ] # Is null?
then
echo "Timed out, variable still unset."
else
echo "variable = $variable"
fi
exit 0
$UID
User ID number
This is the current user's real id, even if she has temporarily assumed another identity through su.
$UID is a readonly variable, not subject to change from the command line or within a script, and is
the counterpart to the id builtin.
#!/bin/bash
# am-i-root.sh: Am I root or not?
if [ "$UID" -eq "$ROOT_UID" ] # Will the real "root" please stand up?
then
echo "You are root."
else
echo "You are just an ordinary user (but mom loves you just the same)."
fi
exit 0
# ============================================================= #
# Code below will not execute, because the script already exited.
ROOTUSER_NAME=root
The variables $ENV, $LOGNAME, $MAIL, $TERM, $USER, and $USERNAME are not
Bash builtins. These are, however, often set as environmental variables in one of the
Bash startup files. $SHELL, the name of the user's login shell, may be set from
/etc/passwd or in an "init" script, and it is likewise not a Bash builtin.
Positional Parameters
#!/bin/bash
# arglist.sh
# Invoke this script with several arguments, such as "one two three".
E_BADARGS=65
echo
echo
echo
exit 0
Following a shift, the $@ holds the remaining command-line parameters, lacking the previous $1,
which was lost.
#!/bin/bash
# Invoke with ./scriptname 1 2 3 4 5
echo "$@" # 1 2 3 4 5
shift
echo "$@" # 2 3 4 5
shift
echo "$@" # 3 4 5
#!/bin/bash
echo
IFS=:
echo 'IFS=":", using "$*"'
c=0
for i in "$*"
do echo "$((c+=1)): [$i]"
done
echo ---
var=$*
echo 'IFS=":", using "$var" (var=$*)'
c=0
for i in "$var"
do echo "$((c+=1)): [$i]"
done
echo ---
var="$*"
echo 'IFS=":", using $var (var="$*")'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo ---
var=$@
echo 'IFS=":", using $var (var=$@)'
c=0
for i in $var
do echo "$((c+=1)): [$i]"
done
echo ---
echo
exit 0
#!/bin/bash
mecho $@ # a,b,c
mecho "$@" # a,b,c
exit
$-
Flags passed to script (using set). See Example 14-16.
This was originally a ksh construct adopted into Bash, and unfortunately it does not
seem to work reliably in Bash scripts. One possible use for it is to have a script
self-test whether it is interactive.
$!
PID (process ID) of last job run in background
LOG=$0.log
COMMAND1="sleep 100"
echo "Logging PIDs background commands for script: $0" >> "$LOG"
# So they can be monitored, and killed as necessary.
echo >> "$LOG"
# Logging commands.
# Thank you, Sylvain Fourmanoit, for this creative use of the "!" variable.
Or, alternately:
possibly_hanging_job & {
while ((count < TIMEOUT )); do
eval '[ ! -d "/proc/$!" ] && ((count = TIMEOUT))'
# /proc is where information about running processes is found.
# "-d" tests whether it exists (whether directory exists).
# So, we're waiting for the job in question to show up.
((count++))
sleep 1
done
eval '[ -d "/proc/$!" ] && kill -15 $!'
# If the hanging job is running, kill it.
}
$_
Special variable set to final argument of previous command executed.
echo $_ # /bin/bash
# Just called /bin/bash to run the script.
# Note that this will vary according to
#+ how the script is invoked.
:
echo $_ # :
$?
Exit status of a command, function, or the script itself (see Example 23-7)
$$
Process ID (PID) of the script itself. [39] The $$ variable often finds use in scripts to construct
"unique" temp file names (see Example 29-6, Example 15-31, and Example 14-27). This is usually
simpler than invoking mktemp.
Bash supports a surprising number of string manipulation operations. Unfortunately, these tools lack a unified
focus. Some are a subset of parameter substitution, and others fall under the functionality of the UNIX expr
command. This results in inconsistent command syntax and overlap of functionality, not to mention
confusion.
String Length
${#string}
expr length $string
These are the equivalent of strlen() in C.
expr "$string" : '.*'
stringZ=abcABC123ABCabc
echo ${#stringZ} # 15
echo `expr length $stringZ` # 15
echo `expr "$stringZ" : '.*'` # 15
#!/bin/bash
# paragraph-space.sh
# Ver. 2.0, Reldate 05Aug08
while read line # For as many lines as the input file has...
do
echo "$line" # Output the line itself.
len=${#line}
if [[ "$len" -lt "$MINLEN" && "$line" =~ \[*\.\] ]]
then echo # Add a blank line immediately
fi #+ after short line terminated by a period.
done
exit
# Exercises:
# ---------
# 1) The script usually inserts a blank line at the end
#+ of the target file. Fix this.
# 2) Line 17 only considers periods as sentence terminators.
# Modify this to include other common end-of-sentence characters,
#+ such as ?, !, and ".
stringZ=abcABC123ABCabc
# |------|
# 12345678
Index
stringZ=abcABC123ABCabc
# 123456 ...
echo `expr index "$stringZ" C12` # 6
# C position.
Substring Extraction
${string:position}
Extracts substring from $string at $position.
If the $string parameter is "*" or "@", then this extracts the positional parameters, [40] starting at
$position.
${string:position:length}
Extracts $length characters of substring from $string at $position.
stringZ=abcABC123ABCabc
# 0123456789.....
# 0-based indexing.
#!/bin/bash
# rand-string.sh
# Generating an 8-character "random" string.
randstring="${str1:$POS:$LEN}"
# Can parameterize ^^^^ ^^^^
echo "$randstring"
If the $string parameter is "*" or "@", then this extracts a maximum of $length positional
parameters, starting at $position.
stringZ=abcABC123ABCabc
# 123456789......
# 1-based indexing.
stringZ=abcABC123ABCabc
# =======
stringZ=abcABC123ABCabc
# ======
Substring Removal
${string#substring}
Deletes shortest match of $substring from front of $string.
${string##substring}
Deletes longest match of $substring from front of $string.
stringZ=abcABC123ABCabc
For example:
SUFF=TXT
suff=txt
stringZ=abcABC123ABCabc
# || shortest
# |------------| longest
echo ${stringZ%%b*c} # a
# Strip out longest match between 'b' and 'c', from back of $stringZ.
This operator is useful for generating filenames.
#!/bin/bash
# cvt.sh:
# Converts all the MacPaint image files in a directory to "pbm" format.
OPERATION=macptopbm
SUFFIX=pbm # New filename suffix.
if [ -n "$1" ]
then
directory=$1 # If directory name given as a script argument...
# Assumes all files in the target directory are MacPaint image files,
#+ with a ".mac" filename suffix.
exit 0
# Exercise:
# --------
# As it stands, this script converts *all* the files in the current
#+ working directory.
# Modify it to work *only* on files with a ".mac" suffix.
#!/bin/bash
# ra2ogg.sh: Convert streaming audio files (*.ra) to ogg.
##########################################################################
mplayer "$1" -ao pcm:file=$OUTFILE
oggenc "$OUTFILE" # Correct file extension automatically added by oggenc.
##########################################################################
exit $?
# Note:
# ----
# On a Website, simply clicking on a *.ram streaming audio file
#+ usually only downloads the URL of the actual *.ra audio file.
# You can then use "wget" or something similar
#+ to download the *.ra file itself.
# Exercises:
# ---------
# As is, this script converts only *.ra filenames.
# Add flexibility by permitting use of *.ram and other filenames.
#
# If you're really ambitious, expand the script
#+ to do automatic downloads and conversions of streaming audio files.
# Given a URL, batch download streaming audio files (using "wget")
#+ and convert them on the fly.
#!/bin/bash
# getopt-simple.sh
# Author: Chris Morgan
# Used in the ABS Guide with permission.
getopt_simple()
{
echo "getopt_simple()"
echo "Parameters are '$*'"
until [ -z "$1" ]
do
echo "Processing parameter of: '$1'"
if [ ${1:0:1} = '/' ]
then
tmp=${1:1} # Strip off leading '/' . . .
parameter=${tmp%%=*} # Extract name.
value=${tmp##*=} # Extract value.
echo "Parameter: '$parameter', value: '$value'"
eval $parameter=$value
fi
shift
done
}
---
Substring Replacement
${string/substring/replacement}
Replace first match of $substring with $replacement. [41]
${string//substring/replacement}
Replace all matches of $substring with $replacement.
stringZ=abcABC123ABCabc
echo ---------------
echo "$stringZ" # abcABC123ABCabc
echo ---------------
# The string itself is not altered!
echo
stringZ=abcABC123ABCabc
A Bash script may invoke the string manipulation facilities of awk as an alternative to using its built-in
operations.
#!/bin/bash
# substring-extraction.sh
String=23skidoo1
# 012345678 Bash
# 123456789 awk
# Note different string indexing system:
# Bash numbers first character of string as 0.
# Awk numbers first character of string as 1.
echo "----"
# And likewise:
exit 0
Script examples:
1. Example 15-9
2. Example 9-18
3. Example 9-19
4. Example 9-20
5. Example 9-22
6. Example A-36
7. Example A-41
${parameter}
Same as $parameter, i.e., value of the variable parameter. In certain contexts, only the less
ambiguous ${parameter} form works.
your_id=${USER}-on-${HOSTNAME}
echo "$your_id"
#
echo "Old \$PATH = $PATH"
PATH=${PATH}:/opt/bin #Add /opt/bin to $PATH for duration of script.
echo "New \$PATH = $PATH"
${parameter-default}, ${parameter:-default}
If parameter not set, use default.
echo ${username-`whoami`}
# Echoes the result of `whoami`, if variable $username is still unset.
#!/bin/bash
# param-sub.sh
username0=
echo "username0 has been declared, but is set to null."
echo "username0 = ${username0-`whoami`}"
# Will not echo.
echo
username2=
echo "username2 has been declared, but is set to null."
echo "username2 = ${username2:-`whoami`}"
# ^
# Will echo because of :- rather than just - in condition test.
# Compare to first instance, above.
variable=
# variable has been declared, but is set to null.
unset variable
echo "${variable-2}" # 2
echo "${variable:-3}" # 3
exit 0
The default parameter construct finds use in providing "missing" command-line arguments in scripts.
DEFAULT_FILENAME=generic.data
filename=${1:-$DEFAULT_FILENAME}
# If not otherwise specified, the following command block operates
#+ on the file "generic.data".
# Begin-Command-Block
# ...
# ...
# ...
# End-Command-Block
Compare this method with using an and list to supply a default command-line argument.
${parameter=default}, ${parameter:=default}
Both forms nearly equivalent. The : makes a difference only when $parameter has been declared
and is null, [42] as above.
echo ${username=`whoami`}
# Variable "username" is now set to `whoami`.
${parameter+alt_value}, ${parameter:+alt_value}
If parameter set, use alt_value, else use null string.
Both forms nearly equivalent. The : makes a difference only when parameter has been declared
and is null, see below.
a=${param1+xyz}
echo "a = $a" # a =
param3=123
a=${param3+xyz}
echo "a = $a" # a = xyz
echo
echo "###### \${parameter:+alt_value} ########"
echo
a=${param4:+xyz}
echo "a = $a" # a =
param5=
a=${param5:+xyz}
echo "a = $a" # a =
# Different result from a=${param5+xyz}
param6=123
a=${param6:+xyz}
echo "a = $a" # a = xyz
${parameter?err_msg}, ${parameter:?err_msg}
If parameter set, use it, else print err_msg.
Both forms nearly equivalent. The : makes a difference only when parameter has been declared
and is null, as above.
#!/bin/bash
# ------------------------------------------------------
ThisVariable=Value-of-ThisVariable
# Note, by the way, that string variables may be set
#+ to characters disallowed in their names.
: ${ThisVariable?}
echo; echo
echo "You will not see this message, because script already terminated."
HERE=0
exit $HERE # Will NOT exit here.
#!/bin/bash
# usage-message.sh
: ${1?"Usage: $0 ARGUMENT"}
# Script exits here if command-line parameter absent,
#+ with following error message.
# usage-message.sh: 1: Usage: usage-message.sh ARGUMENT
# Check the exit status, both with and without command-line parameter.
# If command-line parameter present, then "$?" is 0.
# If not, then "$?" is 1.
Parameter substitution and/or expansion. The following expressions are the complement to the match in
expr string operations (see Example 15-9). These particular ones are used mostly in parsing file path names.
${#var}
String length (number of characters in $var). For an array, ${#array} is the length of the first
element in the array.
Exceptions:
#!/bin/bash
# length.sh
E_NO_ARGS=65
var01=abcdEFGH28ij
echo "var01 = ${var01}"
echo "Length of var01 = ${#var01}"
# Now, let's try embedding a space.
var02="abcd EFGH28ij"
echo "var02 = ${var02}"
echo "Length of var02 = ${#var02}"
exit 0
${var#Pattern}, ${var##Pattern}
${var#Pattern} Remove from $var the shortest part of $Pattern that matches the front end
of $var.
${var##Pattern} Remove from $var the longest part of $Pattern that matches the front end
of $var.
${var%Pattern} Remove from $var the shortest part of $Pattern that matches the back end
of $var.
${var%%Pattern} Remove from $var the longest part of $Pattern that matches the back end
of $var.
#!/bin/bash
# patt-matching.sh
var1=abcd12345abc6789
pattern1=a*c # * (wild card) matches everything between a - c.
echo
echo "var1 = $var1" # abcd12345abc6789
echo "var1 = ${var1}" # abcd12345abc6789
# (alternate form)
echo "Number of characters in ${var1} = ${#var1}"
echo
echo
exit 0
#!/bin/bash
# rfe.sh: Renaming file extensions.
#
# rfe old_extension new_extension
#
# Example:
# To rename all *.gif files in working directory to *.jpg,
# rfe gif jpg
E_BADARGS=65
case $# in
0|1) # The vertical bar means "or" in this context.
echo "Usage: `basename $0` old_file_suffix new_file_suffix"
exit $E_BADARGS # If 0 or 1 arg, then bail out.
;;
esac
exit 0
If Replacement is omitted, then the first match of Pattern is replaced by nothing, that is,
deleted.
${var//Pattern/Replacement}
Global replacement. All matches of Pattern, within var replaced with Replacement.
As above, if Replacement is omitted, then all occurrences of Pattern are replaced by nothing,
that is, deleted.
#!/bin/bash
var1=abcd-1234-defg
echo "var1 = $var1"
t=${var1#*-*}
echo "var1 (with everything, up to and including first - stripped out) = $t"
# t=${var1#*-} works just the same,
#+ since # matches the shortest string,
#+ and * matches everything preceding, including an empty string.
# (Thanks, Stephane Chazelas, for pointing this out.)
t=${var1##*-*}
echo "If var1 contains a \"-\", returns empty string... var1 = $t"
t=${var1%*-*}
echo "var1 (with everything from the last - on stripped out) = $t"
echo
# -------------------------------------------
path_name=/home/bozo/ideas/thoughts.for.today
# -------------------------------------------
echo "path_name = $path_name"
t=${path_name##/*/}
echo "path_name, stripped of prefixes = $t"
# Same effect as t=`basename $path_name` in this particular case.
# t=${path_name%/}; t=${t##*/} is a more general solution,
#+ but still fails sometimes.
# If $path_name ends with a newline, then `basename $path_name` will not work,
#+ but the above expression will.
# (Thanks, S.C.)
t=${path_name%/*.*}
# Same effect as t=`dirname $path_name`
echo "path_name, stripped of suffixes = $t"
# These will fail in some cases, such as "../", "/foo////", # "foo/", "/".
# Removing suffixes, especially when the basename has no suffix,
#+ but the dirname does, also complicates matters.
# (Thanks, S.C.)
echo
t=${path_name:11}
echo "$path_name, with first 11 chars stripped off = $t"
t=${path_name:11:5}
echo "$path_name, with first 11 chars stripped off, length 5 = $t"
echo
t=${path_name/bozo/clown}
exit 0
${var/#Pattern/Replacement}
If prefix of var matches Pattern, then substitute Replacement for Pattern.
${var/%Pattern/Replacement}
If suffix of var matches Pattern, then substitute Replacement for Pattern.
#!/bin/bash
# var-match.sh:
# Demo of pattern replacement at prefix / suffix of string.
echo
# ----------------------------------------------------
# Must match at beginning / end of string,
#+ otherwise no replacement results.
# ----------------------------------------------------
v3=${v0/#123/000} # Matches, but not at beginning.
echo "v3 = $v3" # abc1234zip1234abc
# NO REPLACEMENT.
v4=${v0/%123/000} # Matches, but not at end.
echo "v4 = $v4" # abc1234zip1234abc
# NO REPLACEMENT.
exit 0
${!varprefix*}, ${!varprefix@}
Matches names of all previously declared variables beginning with varprefix.
xyz23=whatever
xyz24=
echo "---"
abc23=something_else
b=${!abc*}
echo "b = $b" # b = abc23
c=${!b} # Now, the more familiar type of indirect reference.
echo $c # something_else
The declare or typeset builtins, which are exact synonyms, permit modifying the properties of variables. This
is a very weak form of the typing [43] available in certain programming languages. The declare command is
specific to version 2 or later of Bash. The typeset command also works in ksh scripts.
declare/typeset options
-r readonly
(declare -r var1 works the same as readonly var1)
This is the rough equivalent of the C const type qualifier. An attempt to change the value of a
readonly variable fails with an error message.
declare -r var1=1
echo "var1 = $var1" # var1 = 1
declare -i number
# The script will treat subsequent occurrences of "number" as an integer.
number=3
echo "Number = $number" # Number = 3
number=three
echo "Number = $number" # Number = 0
# Tries to evaluate the string "three" as an integer.
Certain arithmetic operations are permitted for declared integer variables without the need for expr or
let.
n=6/3
echo "n = $n" # n = 6/3
declare -i n
n=6/3
echo "n = $n" # n = 2
-a array
declare -f
A declare -f line with no arguments in a script causes a listing of all the functions previously
defined in that script.
declare -f function_name
A declare -f function_name in a script lists just the function named.
-x export
declare -x var3
This declares a variable as available for exporting outside the environment of the script itself.
-x var=$value
declare -x var3=373
The declare command permits assigning a value to a variable in the same statement as setting its
properties.
#!/bin/bash
func1 ()
{
echo This is a function.
}
echo
echo
foo ()
{
FOO="bar"
}
bar ()
{
foo
echo $FOO
}
foo (){
declare FOO="bar"
}
bar ()
{
foo
echo $FOO
}
bash$ zzy=68
bash$ declare | grep zzy
zzy=68
We have seen that referencing a variable, $var, fetches its value. But, what about the value of a value? What
about $$var?
The actual notation is \$$var, usually preceded by an eval (and sometimes an echo). This is called an
indirect reference.
#!/bin/bash
# ind-ref.sh: Indirect variable referencing.
# Accessing the contents of the contents of a variable.
var=23
# ============================================== #
echo
# Direct reference.
echo "a = $a" # a = letter_of_alphabet
# Indirect reference.
eval a=\$$a
# ^^^ Forcing an eval(uation), and ...
# ^ Escaping the first $ ...
# ------------------------------------------------------------------------
# The 'eval' forces an update of $a, sets it to the updated value of \$$a.
# So, we see why 'eval' so often shows up in indirect reference notation.
# ------------------------------------------------------------------------
echo "Now a = $a" # Now a = z
echo
echo
t=table_cell_3
NEW_VAL=387
table_cell_3=$NEW_VAL
echo "Changing value of \"table_cell_3\" to $NEW_VAL."
echo "\"table_cell_3\" now $table_cell_3"
echo -n "dereferenced \"t\" now "; eval echo \$$t
# "eval" takes the two arguments "echo" and "\$$t" (set equal to $table_cell_3)
echo
exit 0
Indirect referencing in Bash is a multi-step process. First, take the name of a variable: varname. Then,
reference it: $varname. Then, reference the reference: $$varname. Then, escape the first $:
\$$varname. Finally, force a reevaluation of the expression and assign it: eval newvar=\$$varname.
Of what practical use is indirect referencing of variables? It gives Bash a little of the functionality of pointers
in C, for instance, in table lookup. And, it also has some other very interesting applications. . . .
Nils Radtke shows how to build "dynamic" variable names and evaluate their contents. This can be useful
when sourcing configuration files.
#!/bin/bash
# ---------------------------------------------
# This could be "sourced" from a separate file.
isdnMyProviderRemoteNet=172.16.0.100
isdnYourProviderRemoteNet=10.0.0.10
isdnOnlineService="MyProvider"
# ---------------------------------------------
# ================================================================
chkMirrorArchs () {
arch="$1";
if [ "$(eval "echo \${$(echo get$(echo -ne $arch |
sed 's/^\(.\).*/\1/g' | tr 'a-z' 'A-Z'; echo $arch |
sed 's/^.\(.*\)/\1/g')):-false}")" = true ]
then
return 0;
else
return 1;
fi;
}
getSparc="true"
unset getIa64
chkMirrorArchs sparc
echo $? # 0
# True
chkMirrorArchs Ia64
echo $? # 1
# False
# Notes:
# -----
# Even the to-be-substituted variable name part is built explicitly.
# The parameters to the chkMirrorArchs calls are all lower case.
# The variable name is composed of two parts: "get" and "Sparc" . . .
#!/bin/bash
ARGS=2
E_WRONGARGS=85
" "$filename"
# Note that awk doesn't need an eval preceding \$$.
# -------------------------------------------------
# End awk script.
exit $?
This method of indirect referencing is a bit tricky. If the second order variable changes its value,
then the first order variable must be properly dereferenced (as in the above example).
Fortunately, the ${!variable} notation introduced with version 2 of Bash (see Example
34-2 and Example A-22) makes indirect referencing more intuitive.
Bash does not support pointer arithmetic, and this severely limits the usefulness of indirect referencing. In
fact, indirect referencing in a scripting language is, at best, something of an afterthought.
$RANDOM is an internal Bash function (not a constant) that returns a pseudorandom [44] integer in the range 0
- 32767. It should not be used to generate an encryption key.
#!/bin/bash
MAXCOUNT=10
count=1
echo
echo "$MAXCOUNT random numbers:"
echo "-----------------"
while [ "$count" -le $MAXCOUNT ] # Generate 10 ($MAXCOUNT) random integers.
do
# If you need a random int within a certain range, use the 'modulo' operator.
# This returns the remainder of a division operation.
RANGE=500
echo
number=$RANDOM
let "number %= $RANGE"
# ^^
echo "Random number less than $RANGE --- $number"
echo
FLOOR=200
number=0 #initialize
while [ "$number" -le $FLOOR ]
do
number=$RANDOM
done
echo "Random number greater than $FLOOR --- $number"
echo
# Combine above two techniques to retrieve random number between two limits.
number=0 #initialize
while [ "$number" -le $FLOOR ]
do
number=$RANDOM
let "number %= $RANGE" # Scales $number down within $RANGE.
done
echo "Random number between $FLOOR and $RANGE --- $number"
echo
echo
exit 0
#!/bin/bash
# pick-card.sh
Suites="Clubs
Diamonds
Hearts
Spades"
Denominations="2
3
4
5
6
7
8
9
10
Jack
Queen
King
Ace"
# $bozo sh pick-cards.sh
# Jack of Clubs
#!/bin/bash
# brownian.sh
# Author: Mendel Cooper
# Reldate: 10/26/07
# License: GPL3
# ----------------------------------------------------------------
# This script models Brownian motion:
#+ the random wanderings of tiny particles in a fluid,
#+ as they are buffeted by random currents and collisions.
#+ This is colloquially known as the "Drunkard's Walk."
Show_Slots () {
echo -n " "
for i in $( seq $NUMSLOTS ) # Pretty-print array elements.
do
printf "%3d" ${Slots[$i]} # Allot three spaces per result.
done
# --------------
# main ()
Initialize_Slots
Run
Show_Slots
# --------------
exit $?
# Exercises:
# ---------
# 1) Show the results in a vertical bar graph, or as an alternative,
#+ a scattergram.
# 2) Alter the script to use /dev/urandom instead of $RANDOM.
# Will this make the results more random?
Jipe points out a set of techniques for generating random numbers within a range.
rnumber=$(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min))
Here Bill presents a versatile function that returns a random number between two specified values.
#!/bin/bash
# random-between.sh
# Random number between two specified values.
# Script by Bill Gradwohl, with minor modifications by the document author.
# Used with permission.
randomBetween() {
# Generates a positive or negative random number
#+ between $min and $max
#+ and divisible by $divisibleBy.
# Gives a "reasonably random" distribution of return values.
#
# Bill Gradwohl - Oct 1, 2003
syntax() {
# Function embedded within function.
echo
echo "Syntax: randomBetween [min] [max] [multiple]"
local min=${1:-0}
local max=${2:-32767}
local divisibleBy=${3:-1}
# Default values assigned, in case parameters not passed to function.
local x
local spread
# Sanity check.
if [ $# -gt 3 -o ${divisibleBy} -eq 0 -o ${min} -eq ${max} ]; then
syntax
return 1
fi
# ---------------------------------------------------------------------
# Now, to do the real work.
# The slight increase will produce the proper distribution for the
#+ end points.
spread=$((max-min))
# Omair Eshkenazi points out that this test is unnecessary,
#+ since max and min have already been switched around.
[ ${spread} -lt 0 ] && spread=$((0-spread))
let spread+=divisibleBy
randomBetweenAnswer=$(((RANDOM%spread)/divisibleBy*divisibleBy+min))
return 0
declare -a answer
minimum=${min}
maximum=${max}
if [ $((minimum/divisibleBy*divisibleBy)) -ne ${minimum} ]; then
if [ ${minimum} -lt 0 ]; then
minimum=$((minimum/divisibleBy*divisibleBy))
else
minimum=$((((minimum/divisibleBy)+1)*divisibleBy))
fi
fi
disp=$((0-minimum))
for ((i=${minimum}; i<=${maximum}; i+=divisibleBy)); do
answer[i+disp]=0
done
# Note that we are specifying min and max in reversed order here to
#+ make the function correct for this case.
exit 0
Just how random is $RANDOM? The best way to test this is to write a script that tracks the distribution of
"random" numbers generated by $RANDOM. Let's roll a $RANDOM die a few times . . .
#!/bin/bash
# How random is RANDOM?
RANDOM=$$ # Reseed the random number generator using script process ID.
print_result ()
{
echo
echo "ones = $ones"
echo "twos = $twos"
echo "threes = $threes"
echo "fours = $fours"
echo "fives = $fives"
echo "sixes = $sixes"
echo
}
update_count()
{
case "$1" in
0) let "ones += 1";; # Since die has no "zero", this corresponds to 1.
1) let "twos += 1";; # And this to 2, etc.
2) let "threes += 1";;
3) let "fours += 1";;
4) let "fives += 1";;
5) let "sixes += 1";;
esac
}
echo
print_result
exit 0
# The scores should distribute fairly evenly, assuming RANDOM is fairly random.
# With $MAXTHROWS at 600, all should cluster around 100, plus-or-minus 20 or so.
#
# Keep in mind that RANDOM is a pseudorandom generator,
#+ and not a spectacularly good one at that.
# Exercise (easy):
# ---------------
#!/bin/bash
# seeding-random.sh: Seeding the RANDOM variable.
random_numbers ()
{
count=0
while [ "$count" -lt "$MAXCOUNT" ]
do
number=$RANDOM
echo -n "$number "
let "count += 1"
done
}
echo; echo
echo; echo
echo; echo
echo; echo
# Getting fancy...
SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }')
# Pseudo-random output fetched
#+ from /dev/urandom (system pseudo-random device-file),
#+ then converted to line of printable (octal) numbers by "od",
#+ finally "awk" retrieves just one number for SEED.
RANDOM=$SEED
random_numbers
echo; echo
exit 0
The /dev/urandom pseudo-device file provides a method of generating much more "random"
pseudorandom numbers than the $RANDOM variable. dd if=/dev/urandom of=targetfile
bs=1 count=XX creates a file of well-scattered pseudorandom numbers. However, assigning these
numbers to a variable in a script requires a workaround, such as filtering through od (as in above
example, Example 15-14, and Example A-36), or even piping to md5sum (see Example 33-14).
There are also other ways to generate pseudorandom numbers in a script. Awk provides a convenient
means of doing this.
#!/bin/bash
# random2.sh: Returns a pseudorandom number in the range 0 - 1.
# Uses the awk rand() function.
exit 0
# Exercises:
# ---------
# 3) Same as exercise #2, above, but generate random integers this time.
The date command also lends itself to generating pseudorandom integer sequences.
Similar to the let command, the (( ... )) construct permits arithmetic expansion and evaluation. In its simplest
form, a=$(( 5 + 3 )) would set a to 5 + 3, or 8. However, this double-parentheses construct is also a
mechanism for allowing C-style manipulation of variables in Bash, for example, (( var++ )).