Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting | ||
---|---|---|
Prev |
This index / glossary / quick-reference lists many of the important topics covered in the text. Terms are arranged in approximate ASCII sorting order, reshuffled as necessary for enhanced clarity.
Note that commands are indexed in Part 4.
* * *
^ (caret) Beginning-of-line, in a Regular Expression
~ Tilde
~ home directory, corresponds to $HOME
= Equals sign
= Variable assignment operator
= String comparison operator
== String comparison operator
=~ Regular Expression match operator
< Left angle bracket
Is-less-than
Redirection
< stdin
<<< Here string
<> Opening a file for both reading and writing
> Right angle bracket
Is-greater-than
Integer comparison, within double parentheses
Redirection
> Redirect stdout to a file
>> Redirect stdout to a file, but append
i>&j Redirect file descriptor i to file descriptor j
>&j Redirect stdout to file descriptor j
>&2 Redirect stdout of a command to stderr
2>&1 Redirect stderr to stdout
&> Redirect both stdout and stderr of a command to a file
:> file Truncate file to zero length
| Pipe, a device for passing the output of a command to another command or to the shell
- (dash)
Prefix to default parameter, in parameter substitution
Indicating redirection from stdin or stdout
-- (double-dash)
Prefix to long command options
C-style variable decrement within double parentheses
; (semicolon)
\; Escaped semicolon, terminates a find command
;; Double-semicolon, terminator in a case option
Required when ...
: Colon, null command, equivalent to the true Bash builtin
:> file Truncate file to zero length
! Negation operator, inverts exit status of a test or command
!= not-equal-to String comparison operator
? (question mark)
// Double forward slash, behavior of cd command toward
. (dot / period)
. Load a file (into a script), equivalent to source command
' ... ' (single quotes) strong quoting
" ... " (double quotes) weak quoting
() Parentheses
( ... ) Command group; starts a subshell
( ... ) Enclose group of Extended Regular Expressions
>( ... )
<( ... ) Process substitution
... ) Terminates test-condition in case construct
(( ... )) Double parentheses, in arithmetic expansion
[ Left bracket, test construct
[ ]Brackets
Enclose character set to match in a Regular Expression
[[ ... ]] Double brackets, extended test construct
$ Anchor, in a Regular Expression
$( ... ) Command substitution, setting a variable with output of a command, using parentheses notation
` ... ` Command substitution, using backquotes notation
${ ... } Variable manipulation / evaluation
${var} Value of a variable
${#var} Length of a variable
${#@}
${parameter?err_msg} Parameter-unset message
${parameter-default}
${parameter:-default}
${parameter=default}
${parameter:=default} Set default parameter
${parameter+alt_value}
${parameter:+alt_value}
Alternate value of parameter, if set
${!var}
Indirect referencing of a variable, new notation
${!#}
Final positional parameter. (This is an indirect reference to $#.)
${!varprefix*}
${!varprefix@}
Match names of all previously declared variables beginning with varprefix
${string:position}
${string:position:length} Substring extraction
${var#Pattern}
${var##Pattern} Substring removal
${var%Pattern}
${var%%Pattern} Substring removal
${string/substring/replacement}
${string//substring/replacement}
${string/#substring/replacement}
${string/%substring/replacement} Substring replacement
\ Escape the character following
\< ... \> Angle brackets, escaped, word boundary in a Regular Expression
\{ N \} "Curly" brackets, escaped, number of character sets to match in an Extended RE
\$$ Indirect reverencing of a variable, old-style notation
Escaping a newline, to write a multi-line command
&
&> Redirect both stdout and stderr of a command to a file
>&j Redirect stdout to file descriptor j
>&2 Redirect stdout of a command to stderr
i>&j Redirect file descriptor i to file descriptor j
2>&1 Redirect stderr to stdout
n<&- Close input file descriptor n
0<&-, <&- Close stdin
n>&- Close output file descriptor n
1>&-, >&- Close stdout
Command & Run job in background
# Hashmark, special symbol beginning a script comment
#! Sha-bang, special string starting a shell script
* Asterisk
** Exponentiation, arithmetic operator
% Percent sign
Modulo, division-remainder arithmetic operation
Substring removal (pattern matching) operator
+ Plus sign
Prefix to alternate parameter, in parameter substitution
++ C-style variable increment, within double parentheses
* * *
Shell Variables
$_ Last argument to previous command
$- Flags passed to script, using set
$! Process ID of last background job
$@ All the positional parameters, as separate words
$* All the positional parameters, as a single word
$$ Process ID of the script
$# Number of arguments passed to a function, or to the script itself
$1 First argument passed to script
$9 Ninth argument passed to script
Table of shell variables
* * * * * *
-a Logical AND compound comparison test
Address database, script example
Advanced Bash Scripting Guide, where to download
Removing an alias, using unalias
Angle brackets, escaped, \< . . . \> word boundary in a Regular Expression
Anonymous here document, using :
combination operators, C-style
+= -= *= /= %=
In certain contexts, += can also function as a string concatenation operator. |
Concatenating, example script
declare -a array_name
Empty arrays, empty elements, example script
array=( element1 element2 ... elementN)
Using command substitution
Loading a file into an array
Multidimensional, simulating
${#array_name[@]}
${#array_name[*]}
Passing an array to a function
Special properties, example script
String operations, example script
Arrow keys, detecting
awk field-oriented text processing language
rand(), random function
Using export to pass a variable to an embedded awk script
* * *
Backquotes, used in command substitution
Base conversion, example script
Basics reviewed, script example
Basic commands, external
Batch files, DOS
bc, calculator utility
Template for calculating a script variable
Bison utility
Block devices, testing for
Script example: redirecting output of a a code block
Extended, {a..z}
Brackets, [ ]
Enclose character set to match in a Regular Expression
Brackets, curly, {}, used in
break loop control command
Parameter (optional)
Builtins in Bash
* * *
Command-line parameters, handling
Globbing, filtering strings with
cat, concatentate file(s)
Character devices, testing for
Colon, : , equivalent to the true Bash builtin
Comma operator, linking commands or operations
$( ... ), preferred notation
Comment headers, special purpose
Commenting out blocks of code
Using an anonymous here document
Using an if-then construct
Compound comparison operators
continue loop control command
Control-C, break
Control-D, terminate / log out / erase
Control-G, BEL (beep)
Control-H, rubout
Control-J, newline
Control-M, carriage return
cron, scheduling daemon
C-style syntax , for handling variables
Curly brackets {}
* * *
Daemons, in UNIX-type OS
dc, calculator utility
dd, data duplicator command
Copying raw data to/from devices
File deletion, secure
Keystrokes, capturing
Random access on a data stream
Swapfiles, initializing
Decimal number, Bash interprets numbers as
declare builtin
/dev directory
/dev/null pseudo-device file
/dev/urandom pseudo-device file, generating pseudorandom numbers with
/dev/zero, pseudo-device file
dialog, utility for generating dialog boxes in a script
$DIRSTACK directory stack
Disabled commands, in restricted shells
do keyword, begins execution of commands within a loop
done keyword, terminates a loop
DOS batch files, converting to shell scripts
DOS commands, UNIX equivalents of (table)
dot files, "hidden" setup and configuration files
Double brackets [[ ... ]] test construct
Double quotes " ... " weak quoting
Double-spacing a text file, using sed
* * *
-e File exists test
/bin/echo, external echo command
elif, Contraction of else and if
esac, keyword terminating case construct
-eq , is-equal-to integer comparison test
Eratosthenes, Sieve of, algorithm for generating prime numbers
Escaped characters, special meanings of
$EUID, Effective user ID
eval, Combine and evaluate expression(s), with variable expansion
Effects of, Example script
Forces reevaluation of arguments
Evaluation of octal/hex constants within [[ ... ]]
exec command, using in redirection
Exit and Exit status
exit command
Exit status (exit code, return status of a command)
Table, Exit codes with special meanings
Specified by a function return
Successful, 0
/usr/include/sysexits.h, system file listing C/C++ standard exit codes
Export, to make available variables to child processes
expr, Expression evaluator
? (question mark) Match zero / one characters
( ... ) Group of expressions
\{ N \} "Curly" brackets, escaped, number of character sets to match
* * *
factor, decomposes an integer into its prime factors
Application: Generating prime numbers
false, returns unsuccessful (1) exit status
n<&- Close input file descriptor n
0<&-, <&- Close stdin
n>&- Close output file descriptor n
1>&-, >&- Close stdout
File handles in C, similarity to
Floating point numbers, Bash does not recognize
fold, a filter to wrap lines of text
Forking a child process
Arguments passed referred to by position
Capturing the return value of a function using echo
Definition must precede first call to function
and recursion
Passing an array to a function
Passing pointers to a function
Redirecting stdin of a function
Multiple return values from a function, example script
Returning an array from a function
return range limits, workarounds
shift arguments passed to a function
* * *
Games and amusements
Anagrams, again
getopt, external command for parsing script command-line arguments
getopts, Bash builtin for parsing script command-line arguments
Global variable
Globbing, filename expansion
-ge , greater-than or equal integer comparison test
-gt , greater-than integer comparison test
groff, text markup and formatting language
$GROUPS, Groups user belongs to
gzip, compression utility
* * *
Hashing, creating lookup keys in a table
head, echo to stdout lines at the beginning of a text file
help, gives usage summary of a Bash builtin
Anonymous here documents, using :
Commenting out blocks of code
Self-documenting scripts
Function, supplying input to
Closing limit string may not be indented
Dash option to limit string, <<-LimitString
Literal text output, for generating program code
Disabling parameter substitution
$HOME, user's home directory
$HOSTNAME, system host name
* * *
$Id parameter, in rcs (Revision Control System)
if [ condition ]; then ... test construct
$IFS, Internal field separator variable
in, keyword preceding [list] in a for loop
Initialization table, /etc/inittab
Inline group, i.e., code block
Interactive script, test for
Indirect referencing of variables
New notation, introduced in version 2 of Bash ( example script)
* * *
Job IDs, table
jot, Emit a sequence of integers. Equivalent to seq.
* * *
kill, terminate a process by process ID
Options (-l, -9)
killall, terminate a process by name
killall script in /etc/rc.d/init.d
* * *
-le , less-than or equal integer comparison test
let, setting and carrying out arithmetic operations on variables
Limit string, in a here document
$LINENO, variable indicating the line number where it appears in a script
Link, file (using ln command)
Invoking script with multiple names, using ln
symbolic links, ln -s
and recursion
Logical operators (&&, ||, etc.)
Logout file, the ~/.bash_logout file
break loop control command
continue loop control command
C-style loop within double parentheses
do (keyword), begins execution of commands within a loop
done (keyword), terminates a loop
for arg in [list]; do
Command substitution to generate [list]
Multiple parameters in each [list] element
Omitting [list], defaults to positional parameters
in, (keyword) preceding [list] in a for loop
Running a loop in the background, script example
Semicolon required, when do is on first line of loop
until loop
until [ condition-is-true ]; do
while loop
while [ condition ]; do
Function call inside test brackets
while read construct
Loopback devices
-lt , less-than integer comparison test
* * *
m4, macro processing language
$MACHTYPE, Machine type
Magic number, marker at the head of a file indicating the file type
Makefile, file containing the list of dependencies used by make command
Man page editor (script)
Modulo, arithmetic remainder operator
Application: Generating prime numbers
Mortgage calculations, example script
* * *
-n String not null test
Named pipe, a temporary FIFO buffer
nc, netcat, a toolkit for TCP and UDP ports
-ne, not-equal-to integer comparison test
Negation operator, !, reverses the sense of a test
netstat, Network statistics
nl, a filter to number lines of text
Noclobber, -C option to Bash to prevent overwriting of files
null variable assignment, avoiding
* * *
-o Logical OR compound comparison test
od, octal dump
$OLDPWD Previous working directory
Operator
Options, passed to shell or script on command line or by set command
* * *
${parameter+alt_value}
${parameter:+alt_value}
Alternate value of parameter, if set
${parameter-default}
${parameter:-default}
${parameter=default}
${parameter:=default}
${!varprefix*}
${!varprefix@}
${parameter?err_msg}
${parameter}
Table of parameter substitution
Parent / child process problem, a child process cannot export variables to a parent process
Parentheses
Enclose group of Extended Regular Expressions
Double parentheses, in arithmetic expansion
$PATH, the path (location of system binaries)
Perl, programming language
Perquackey-type anagramming game (Quackey script)
PID, Process ID, an identification number assigned to a running process.
Pipe, | , a device for passing the output of a command to another command or to the shell
Avoiding unnecessary commands in a pipe
Pipefail, set -o pipefail option to indicate exit status within a pipe
$PIPESTATUS, exit status of last executed pipe
Piping output of a command to a script
Redirecting stdin, rather than using cat in a pipe
// (double forward slash), behavior of cd command toward
#!/bin/sh script header disables extended Bash features
CGI programming, using scripts for
Closing limit string in a here document, indenting
DOS-type newlines (\r\n) crash a script
eval, risk of using
Execute permission lacking for commands within a script
Export problem, child process to parent process
Extended Bash features not available
Failing to quote variables within test brackets
GNU command set, in cross-platform scripts
Multiple echo statements in a function whose output is captured
Numerical and string comparison operators not equivalent
= and -eq not interchangeable
Omitting terminal semicolon, in a curly-bracketed code block
Piping
Preserving whitespace within a variable, unintended consequences
Undocumented Bash features, danger of
Variable names, inappropriate
Variables in a subshell, scope limited
Whitespace, misuse of
Pointers
Portability issues in shell scripting
POSIX, Portable Operating System Interface / UNIX
$PPID, process ID of parent process
Precedence, operator
Prepending lines at head of a file, script example
Prime numbers
Generating primes using the factor command
Generating primes using the modulo operator
Sieve of Eratosthenes, example script
printf, formatted print command
/proc directory
Running processes, files describing
Writing to files in /proc, warning
Prompt
Pseudo-code, as problem-solving method
$PWD, Current working directory
* * *
Quackey, a Perquackey-type anagramming game (script)
Question mark, ?
Character match in an Extended Regular Expression
Whitespace, using quoting to preserve
* * *
Random numbers
$RANDOM, Bash function that returns a pseudorandom integer
Random sequence generation, using date command
Random sequence generation, using jot
Random string, generating
read, set value of a variable from stdin
Piping output of cat to read
Problems piping echo to read
Redirection from a file to read
$REPLY, default read variable
while read construct
Redirection
to reassign file descriptors
Introductory-level explanation of I/O redirection
Open a file for both reading and writing
<>filename
read input redirected from a file
2>&1
stdin / stdout, using -
> ... >>
>&j
file descriptori to file descriptor j
i>&j
stdout of a command to stderr
>&2
stdout and stderr of a command to a file
&>
tee, redirect to a file output of command(s) partway through a pipe
Test operators
^ (caret) Beginning-of-line
$ (dollar sign) Anchor
. (dot) Match single character
* (asterisk) Any number of characters
[ ] (brackets) Enclose character set to match
\ (backslash) Escape, interpret following character literally
\< ... \> (angle brackets, escaped) Word boundary
Extended REs
\{ \} Escaped "curly" brackets
[: :] POSIX character classes
$REPLY, Default value associated with read command
Restricted shell, shell (or script) with certain commands disabled
return, command that terminates a function
Running scripts in sequence, without user intervention
* * *
Scope of a variable, definition
Script options, set at command line
Scripting routines, library of useful definitions and functions
Secondary prompt, $PS2
nmap, network mapper / port scanner
Viruses, trojans, and worms in scripts
sed, pattern-based programming language
select, construct for menu building
Semicolon required, when do keyword is on first line of loop
seq, Emit a sequence of integers. Equivalent to jot.
set, Change value of internal script variables
Shell script, definition of
Shell wrapper, script embedding a command or utility
shift, reassigning positional parameters
$SHLVL, shell level, depth to which the shell (or script) is nested
shopt, change shell options
Signal, a message sent to a process
Simulations
Single quotes (' ... ') strong quoting
Socket, a communication node associated with an I/O port
Sorting
source, execute a script or, within a script, import a file
Spam, dealing with
Stack, emulating a push-down, example script
Standard Deviation, example script
Startup files, Bash
Strings
${#string}
Manipulation, using awk
Protecting strings from expansion and/or reinterpretation, script example
Unprotecting strings, script example
strchr(), equivalent of
strlen(), equivalent of
strings command, find printable strings in a binary or data file
Substring extraction
Substring index (numerical position in string)
Substring matching, using expr
Substring replacement
${string/substring/replacement}
${string//substring/replacement}
${string/#substring/replacement}
Table of string/substring manipulation and extraction operators
Strong quoting ' ... '
Stylesheet for writing scripts
Variables, $BASH_SUBSHELL and $SHLVL
Variables in a subshell
scope limited, but ...
su Substitute user, log on as a different user or as root
suid (set user id) file flag
suid commands inside a script, not advisable
* * *
Table lookup, script example
tail, echo to stdout lines at the (tail) end of a text file
tar, archiving utility
tee, redirect to a file output of command(s) partway through a pipe
test command
external command, /usr/bin/test (equivalent to /usr/bin/[)
Test operators
-a Logical AND compound comparison
-e File exists
-eq is-equal-to (integer comparison)
-ge greater-than or equal (integer comparison)
-gt greater-than (integer comparison)
-le less-than or equal (integer comparison)
-lt less-than (integer comparison)
-n not-zero-length (string comparison)
-ne not-equal-to (integer comparison)
-o Logical OR compound comparison
-u suid flag set, file test
-z is-zero-length (string comparison)
= is-equal-to (string comparison)
== is-equal-to (string comparison)
< less-than (string comparison)
< less-than, (integer comparison, within double parentheses)
<= less-than-or-equal, (integer comparison, within double parentheses)
> greater-than (string comparison)
> greater-than, (integer comparison, within double parentheses)
>= greater-than-or-equal, (integer comparison, within double parentheses)
|| Logical OR
&& Logical AND
! Negation operator, inverts exit status of a test
!= not-equal-to (string comparison)
Tables of test operators
Timed input
Tips and hints for Bash scripts
Capturing the return value of a function, using echo
Comment blocks
Using anonymous here documents
Using if-then constructs
Comment headers, special purpose
C-style syntax , for manipulating variables
Filenames prefixed with a dash, removing
Filter, feeding output back to same filter
Function return value workarounds
Library of useful definitions and functions
null variable assignment, avoiding
Passing an array to a function
Prepending lines at head of a file
Progress bar template
Running scripts in sequence without user intervention, using run-parts
Script as embedded command
Script portability
Setting script variable to a block of embedded sed or awk code
Subshell variable, accessing outside the subshell
Testing a variable to see if it contains only digits
Widgets, invoking from a script
$TMOUT, Timeout interval
tput, terminal-control command
tr, character translation filter
Soundex, example script
Trap, specifying an action upon receipt of a signal
Trinary operator, C-style, var>10?88:99
true, returns successful (0) exit status
typeset builtin
* * *
$UID, User ID number
uname, output system information
uniq, filter to remove duplicate lines from a sorted file
unset, delete a shell variable
until loop
until [ condition-is-true ]; do
* * *
Variables
Block of sed or awk code, setting a variable to
declare, to modify the properties of variables
Deleting a shell variable using unset
Expansion / Substring replacement operators
eval variable1=\$$variable2
${!variable}
${#var}
Name and value of a variable, distinguishing between
null variable assignment, avoiding
In subshell not visible to parent shell
Testing a variable if it contains only digits
Typing, restricting the properties of a variable
Undeclared, error message
Unquoted variable, splitting
* * *
wait, suspend script execution
Weak quoting " ... "
while loop
while [ condition ]; do
while read construct
Whitespace, spaces, tabs, and newline characters
Preceding closing limit string in a here document, error
Quoting, to preserve whitespace within strings or variables
[:space:], POSIX character class
who, information about logged on users
Wild card characters
Wrapper, shell
* * *
xargs, Filter for grouping arguments
Processes arguments one at a time
Whitespace, handling
* * *
* * *
Zombie, a process that has terminated, but not yet been killed by its parent