UNIX Bash_Quick_Reference

DEFINITIONS

This card describes version 2.02.0 of bash. Several typefaces are used to clarify the meaning:
 * Serifa Bold is used for computer input.
 * Serifa Italic is used to indicate user input and for syntactic placeholders, such as variable or cmd.
 * Serifa Roman is used for explanatory text.

blank - separator between words. Blanks consist of one or more spaces and/or tab characters. In addition, words are terminated by any of the following characters:
;&()|<> space tab newline

command - a series of words.

list - one or more pipelines. Can be separated by ;, &, &&, || and optionally be terminated by ;, &.

n - an integer.

name - a variable, alias, function or command name.

keyword - a reserved word in the bash language. Keywords are special only after a ; or newline, after another keyword, and incertain other contexts.

pat - a bash pattern See Patterns.

pipeline - a command or multiple commands connected by a pipe (|).

string - a collection of characters treated as a unit.

substitution - the process of replacing parts of the command line with different, text, e.g, replacing a variable with its value. bash performs many substitutions. This card lists them in the order they are performed.

word - a generic argument; a word. Quoting may be necessary if it, contains special characters.


RESTRICTED bash

If bash is invoked as rbash, or with the -r option, it is restricted. The following actions are not allow ed in a restricted shell:

changing directory with cd
setting or unsetting $SHELL or $PATH
using path names for commands that contain /
using a path name that contains / for the .command
importing functions from the environment
parsing $SHELLOPTS at startup
redirecting output with any of >, >|, <>, >&, &>, or »
using exec to run a different command
adding or deleting built-incommands with enable
using command -p to bypass a restricted $PATH
using set +r or set +o restricted

These restrictions are in effect after executing all startup files, allowing the author of the startup files full control in setting up the restricted environment. (In practice, restricted shells are not used much, as they are difficult to set up correctly.)


COMMAND LINE ARGUMENTS

bash accepts the one letter options to set, and the additional one letter and GNU-style long options shown below.

$bash[option][args]
- : ends option processing
– : ends option processing
-c cmd : execute cmd (default reads command from file named in first entry of args and found via path search)
-D : print all double quoted strings that are preceded by a $ to stdout. This implies -n, no commands are executed
-i : set interactive mode
-r : set restricted mode
-s : read commands from stdin(defualt)
–dump-po-strings : same as -D, but output in GNU gettext format
–dump-strings : same as -D
–help : display a help message and exit successfully
–login : act like a login shell
–noediting : do not use the readline library to read commands when interactive
–noprofile : do not read any of the initialization files. See Invocation And Startup, below
–norc : do not read “/.bashrc if interactive. See In/ocation And Startup, below
–posix : follow the IEEE POSIX 1003.2 standard
–rcfile file : use file instead of ”/.bashrc if interactive
–restricted : same as -r
–verbose : same as set -v
–version : print version information on stdout and exit successfully


INVOCATION AND START UP

There are five ways that bash runs: normal interactive, normal non-interactive, as sh, in POSIX mode, or invoked via rshd.

1. Normal inractive: Login shells run commands in /etc/profile. The first of “/.bash_profile, ”/.bash_login, and “/.profile that is found is executed. This stage is skipped if –noprofile is used.

Upon log out, bash runs ”/.bash_log out if it exists.

Interactive non-login shells execute “/.bashrc, if it exists. The –rcfile option changes the file that is used.

2. Normal non-interactive: Non-interactive shells do variable, command, and arithmetic substitution on the value of $BASH_ENV, and if the result names an existing file, that file is executed.

3. Invoked as sh: Interactive login shells read and execute /etc/profile and ”/.profile if they exist. These files are skipped if –noprofile is used. Interactive shells expand $ENV and execute that file if it exists. Non-interactive shells do not read any start up files. After the start up files are executed, bash enters POSIX mode.

4. POSIX mode: When started with –posix, interactive shells expand $ENV and execute the given file. No other start up files are read.

5. Invoked via rshd: If run from rshd and not invoked as sh, bash reads “/.bashrc. The –norc option skips this step, and the –rcfile option changes the file, but rshd usually does not pass these options on to the shell it invokes.

If $SHELLOPTS exists in the environment at, start up, bash enables the given options.


PROMPTING

When interactive, bash displays the primary and secondary prompt strings, $PS1 and $PS2. bash expands the following escape sequences in the values of these strings.

\a : an ASCII BEL character (octal 07)
\d : the detein “Weekday Month Day” format
\e : an ASCII escape character (octal 033)
\h : the hostname up to the first dot(.)
\H : the full hostname
\n : a newline
\r : a carriage return
\s : the name of the shell (basename of $0)
\t : the time in 24-hour HH:MM:SS format
\T : the time in 12-hour HH:MM:SS format
\u : the user's username
\v : the version of bash (e.g., 2.02)
\V : the version and patchlevel of bash(e.g., 2.02.0)
\w : the current working directory
\W : the basename of the current working directory
\! : the history number of this command
\# : the command number of this command
\$ : a # if the effective UID is 0, otherwise a $
\@ : the time in 12-hour am/pm format
\\ : a backslash
\nnn : the character corresponding to octal value nnn
\[ : start a sequence of non-printing characters
\] : end a sequence of non-printing characters

The history number is the number of the command in the history list, which may include commands restored from the history file. The command number is the number of this command starting from the first command run by the current invocation of the shell.

The default value of PS1 is “\s-\v\$”.


HISTORY SUBSTITUTION

History expansion is similar to csh's. It is enabled by default in interactive shells. History expansion happens before the shell breaks the input into words, although quoting is recognized and quoted text is treated as one history “word”.

History substitution is performed on history events, which consist of an event designator (which previous line to start with), a word designator (which word from that line to use, starting with zero), and one or more optional modifiers (which parts of the words to use). Colons separate the three parts, although the colon between the event designator and word designator may be omitted when the word designator begins with ”,$,*,-,or %. Each modifier is separated from the next one with a colon. The histchars variable specifies the start-of-history and quick substitution characters, and also the comment character that indicates that the rest of a line is a comment. The previous command is the default event if no event designator is supplied.

The event designators are:
! : start a history substitution
!n : command line n
!-n : current line minus n (n previous)
!! : the previous command
!str : most recent command line starting with str
!/?str[?] : most recent command line containing str
!# : the entire command line typed so far
^old^new^ : quick substitution: repeat last command changing old to new

The word designators are:
0 the zero'th word (command name)
n : word n

$ : the last argument
% : the word matched by the most recent !?str?search
x-y : words x through y. -y is short for 0-y
* : words 1 through the last (like 1-$)
n* : words n through the last (like n-$)
n- : words n through the next to last

The modifiers are:
e : remove all but the suffix of a filename
g : make changes globally, use with s modifier, below
h : remove the last part of a filename, leaving the “head”
p : print the command but do not execute it
q : quote the generated text
r : remove the last suffix of a filename
s/old/new/ : substitue new for old in the text. Any delimiter may be used. An & in the replacement means the value of old. With empty old, use last old, or the most recent !?str? search if there was no previous old
t : remove all but the last part of a filename, leaving the “tail”
x : quote the generated text, but break into words at blanks and newline
& : repeat the last substitution


QUOTING

\c : quote single character c
'…' : old style command substitution
“…” : text treated as a single argument, double quotes removed; variable, command and arithmetic substitutions performed; use \ to quote $, \, ', and “
$”…“ : like ”…“, but locale tr anslation done
'…' : text treated as a single argument, single quotes removed; text between quotes left alone, cannot include '
$'…' : text treatedas a single argument, $ and single quotes removed; no substitutions performed; ANSI C and additional escape sequences processed:

\a alert (bell)
\b backspace
\f form feed
\n newline
\r carriage return
\t horizontal tab
\v vertical tab
\ddd octal value ddd
\xhhh hex value hhh
\\ backslash
\e escape, not in ANSI C


ALIASING

alias name=value …

Aliases are expanded when a command is read, not when executed. Alias names can contain any non-special character, not just alphanumerics, except for = Alias expansion is done on the first word of a command. If the last character of the replacement text is a blank, then the next word in the command line is checked for alias expansion. Aliases can even be used to redefine shell keywords, but not in POSIX mode.


BRACE EXPANSION

Brace expansion is similar to csh's. A word must contain at least one unquoted left brace and comma to be expanded. bash expands the comma-separated items in order, the result is not sorted. Brace expansions may be nested. For example:

$ mkdir /usr/{gnu,local]/{src,bin,lib}


TILDE SUBSTITUTION

~ : substitute $HOME
~user : substitute user's home directory
~+ : substitute $PWD
~- : substitute $(DIRSTACK[n]}. A leading + or - is allowed: negative values count from the end of the stack

Tilde substitution happens after alias expansion. It is done for words that begin with ~ and for variable assignment.

In variable assignments, it is also done after a : in the value. Tilde substitution is done as part of word expansion. This means for ${name op word}, word will be checked for tilde substitutionm but only if the operation requires the value of the right-hand side.


VARIABLE SUBSTITUTION

$name : reference to shell variable name
${name} : use braces to delimit shell variable name
${name-word} : use variable name if set, else use word
${name=word} : as above but also set name to word
${name?word} : use name if set, otherwise print word and exit (interactive shells do not exit)
${name+word} : use word if name is set, otherwise use nothing
${name[n]} : element n in array name
${#name} : length of shell variable name
${#name[*]} : number of elements in array name
${#name[@]} : number of elements in array name
${name#pat} : remove shortest leading substring of name that matches pat
${name##pat} : remove longest leading substring of name that matches pat
${name%pat} : remove shortest trailing substring of name that matches pat
${name%%pat} : remove longest trailing substring of name that matches pat
${name:start}
${name:start:length} : length characters of names starting at start (counting from 0); use rest of value if no length. Negative start counts from the end. If name is * or @ or an array indexed by * or @, start and length indicate the array index and count of elements. start and length can be arithmetic expressions
${name/pattern/string} : value of name with first match of pattern replaced with string
${name/pattern} : value of name with first match of pattern deleted
${name//pattern/string} : value of name with every match of pattern replaced with string
${name/#pattern/string} : value of name with match of pattern replaced with string; match must occur at beginning
${name/%pattern/string} : value of name with match of pattern replaced with string; match occurs at end

Note: for -, =, ?, and +, using name: instead of name tests whether name is set and non-NULL; using name tests only whether name is set.

For #, ##, %, %%, /, //, /#, and /%, when name is * or @ or an array indexed by * or @, the substring or substitution operation is applied to each element.


ARITHMETIC EVALUATION

Arithmetic evaluation is done with the let built-in command, the ((...)) command and the $((...)) expansion for producing the result of an expression.

All arithmetic uses long integers. Use typeset -i to get integer variables. Integer constants look like [base#]n where base is a decimal number between two and 64, and n is in that base. The digits are 0-9, a-z A-Z, _and @. A leadiing 0 or 0x denote octal or hexadecimal.

The following operators based on C, with the same precedence and associativity, are available.

+ - : unary plus and minus
! ~ : logical and bitwise negation
** : exponentiation (not in C)
* / % : multiply, divide, modulus
+ - : addition, subtraction
« » : left shift, right shift
< ⇐ > >= : comparisons
== != : equals, not equals
& : bitwise AND

&& : logical AND, short circuit

?: : in-line conditional
= += -= *= /= %= &= |= ^= «= »= : assignment operators

Inside let, ((...)), and $((...)), variable names do not need a $ to get their values.


COMMAND SUBSTITUTION

$(command) new form
'command' old form

Run command, substitue the results as arguments. Trailing newlines are removed. Characters in $IFS separate words (see Field Splitting). The new form is preferred for simpler quoting rules.

$((expression)) arithmetic substitution

The expression is evaluated, and the result is used as an argument to the current command.


PROCESS SUBSTITUTION

cmd <(list1) >(list2)

Runs list1 and list2 asynchronously, with stdin and stdout respectively connected via pipes using fifos or files in /dev/fd. These file names become arguments to cmd, which expects to read its first, argument, and write its second. This only works if you have /dev/fd or fifos.


FIELD SPLITTING

Quoted text becomes one word. Otherwise, occurrences of any character in $IFS separate words. Multiple whitespace characters that are in $IFS do not delimit empty words, while multiple non-whitespace characters do. When $IFS is not the default value, sequences of leading and trailing $IFS whitespace characters are removed, and printable characters in $IFS surrounded by adjacent $IFS whitespace characters delimit fields. If $IFS is NULL, bash does not do field splitting.


PATTERNS

? : match single character in filename
* : match 0 or more characters in filename
[char] : match any of chars (pair separated by a - matches a range)
[!char] : match any except chars
[^chars] : match any except chars

If the extglob option to shopt is set, the following extended matching facilities may be used.

?(pat-list) : optionally match any of the patterns
*(pat-list) : match 0 or more of any of the patterns
+(pat-list) : match 1 or more of any of the patterns
@(pat-list) : match exactly 1 of any of the patterns
!(pat-list) : match anything but any of the patterns

pat-list is a list of one or more patterns separated by |.

The POSIX [[=c=]] and [[.c.]] notations for same-weight characters and collacting elements are accepted. The notation [[:class:]] defines character classes:
alnum alphanumeric lower lower-case
alpha alphabetic print printable
blank space or tab punct punctuation
cntrl control space whitespace
digit decimal upper upper-case
graph non-spaces xdigit hexadecimal

Three shopt options affect pattern matching.

dotglob : include files whose names begin with.
nocaseglob : ignore case when matching
nullglob : remove patterns that don't match

When expanding filenames, . and .. are ignored, filenames matching the patterns in $GLOBIGNORE are also ignored and a leading. must be supplied in the pattern to match filenames that begin with . . However, setting GLOBIGNORE enables the dotglob option. Include .* in GLOBIGNORE to get the default behavior.


VARIABLE NAMES

Variable names are made up of letters, digits and underscores. They may not start with a digit. There is no limit on the length of a variable name, and the case of letters is significant.


VARIABLE ASSIGNMENT

Assignments to integer variables undergo arithmetic evaluation. Variable assignments have one of the following forms.

name = word set name to word
name[index] = word
set element index of array name to word
name=(word …)
set indexed array name to words
name=([num]=word …)
set given indices of array name to words


PRE-DEFINED VARIABLES

$n : use positional parameter n, n ≤ 9
${n} : use positional parameter n
$* : all positional parameters
$@ : all positional parameters
“$*” : equivalent to “$1 $2 …“
“$@” : equivalent to “$1” “$2” …
$# : number of positional parameters
$- : options to shell or by set
$? : value returned by last command
$$ : process number of current shell
$! : process number of last background cmd
$_ : name of program in environment at startup. Value of last positional argument in last command. Name of changed mail file in $MAILPATH
$auto_resume : enables use of single-word commands to match stopped jobs for foregrounding. With a value of exact, the word must exactly match the command used to start the job. With a value of substring, the typed word can be a substring of the command like %2string
$BASH : full file name used to invoke bash
$BASH_ENV : in normal non-interactive shells only, value is variable, command and arithmetic substitued for path of startup file (See Invocation And Startup)
$BASH_VERSION : the version of bash
$BASH_VERSINFO[0] : the major version number (release)
$BASH_VERSINFO[1] : the minor version number (version)
$BASH_VERSINFO[2] : the patchlevel
$BASH_VERSINFO[3] : the build version
$BASH_VERSINFO[4] : the release status
$BASH_VERSINFO[5] : same as $MACHTYPE
$CDPATH : search path for cd command
$DIRSTACK[*] : array variable containing the pushd and popd directory stack
$ENV : in interactive POSIX mode shells, or when invoked as sh, value is variable, command and arithmetic substitued for path of startup file
$EUID : the effective user id (readonly)
$FCEDIT : default editor for the fc command (no default value)
$FIGNORE : colon-separated list of suffixes giving the set of filenames to ignore when doing filename completion using readline
$GLOBIGNORE : colon-separated list of patterns giving the set of filenames to ignore when doing pattern matching
$GROUPTS[*] : readonly array variable with the list of groups the user belongs to
$histchars : characters that control csh-style history (default: !^#). See History Substitution
$HIST CMD : history number of the current command
$HIST CONTROL : with a value of ignorespace, do not enter lines that begin with spaces into the history file. With a value of ignoredups, do not enter a line that matches the previous line. Use ignoreboth to combine both options
$HIST FILE : where command history is stored
$HISTFILESIZE : maximum number of lines to keep in $HISTFILE
$HISIGNORE : colon-separated list of patterns; if the current line matches any of them, the line is not entered in the history file. & represents the last history line. Patterns must match the whole line
$HISTSIZE : number of previous commands to keep a vailable while bash is running
$HOME : home directory for cd command and value used for tilde expansion
$HOSTFILE : file in format of /etc/hosts to use for hostname completion
$HOSTNAME : name of the current host
$HOSTTYPE : string describing the current host
$IFS : field separators (space, tab, newline)
$IGNOREEOF : for interactive shells, the number of consecutive EOFs that must be entered before bash actually exits
$INPUTRC : name of readline startup file, overrides ~/.inputrc
$LANG : name of current locale
$LC_ALL : current locale; overrides $LANG and other $LC_variables
$LC_COLLATE : current locale for character collation, includes sorting results of filename expansion
$LC_CTYPE : current locale for character class functions (see Patterns)
$LC_MESSAGES current locale for translating $”…” strings
$LINENO : line number of line being executed in script or function
$MACHTYPE : a string in GNU cpu-company-system format describing the machine running bash
$MAIL : name of a mail file, if any
$MAILCHECK : check for mail every n seconds (60 default)
$MAILPATH : filenames to check for new mail; uses: separator; filename may be followed by ?message; $_ in message is matched mail file name. Overrides $MAIL
$OLDPWD : previous working directory
$OPTARG : value of last argument processed by getopts
$OPTERR : if set to 1, display error messages from getopts (default: 1)
$OPTIND : index of last argument processed by getopts
$OSTYPE : string describing the operating system running bash
$PATH : command search path
$PIPESTATUS[*] : array variable containing exit status values from processes in the most recently executed for eground pipline
$PPID : process id of shell's parent
$PROMPT_COMMAND : command to run before each primary prompt
$PS1 : primary prompt string (\s-\v\$)
$PS2 : secondary prompt string (>)
$PS3 : select command prompt string (#?)
$PS4 : tracing prompt string (+)
$PWD : current working directory
$RANDOM : set each time it's referenced, 0 - 32767
$REPLY : set by the select and read commands
$SECONDS : number of seconds since shell invocation
$SHELL : name of this shell
$SHELLOPTS : colon-separated list of the enabled shell options for set -o
$SHLVL : incremented by one for each sub-bash
$TIMEFORMAT : format string for output of time keyword. Special constructs introduced by %.
%[p][l]R elapsed secs
%[p][l]U user CPU secs
%[p][l]S system CPU secs
%P CPU percentage
%% literal %

Optional p gives the precision, the number of digits after the decimal point; it must be between 0 and 3. Optional l produces a longer format, in the form MMmSS.FFs
$TMOUT : number of seconds to wait during prompt before terminating
$UID : the real user id (readonly)


FUNCTIONS

Functions run in the same process as the calling script, and share the open files and current directory. They access their parameters like a script, via $1, $2 and so on. $0 does not change. return may be used inside a function or . script. Functions share traps with the parent script, except for DEBUG. Functions may be recursive, and may have local variables, declared using declare, local, or typeset. Functions may be exported into the environment with export -f.


INPUT / OUTPUT

Redirections are done left to right, after pipes are set up. Default file descriptors are stdin and stdout. File descriptors above 2 are marked close-on-exec.

&>word : send stdout and stderr to word

&word : send stdout and stderr to word

[n]<file : use file for input
[n]>file : use file for output
[n]>|file : like >, but overrides noclobber
[n]»file : like > but append to file if it exists
[n]<>file : open file for read/write (default:fd0)
[n]<&m : duplicate input file descriptor from m
[n]>&m : duplicate output file descriptor from m
[n]<&- : close input file descriptor
[n]>&- : close output file descriptor
[n]«word : input comes from the shell script; treat a line with word as EOF on input. If any of word is quoted, no additional processing is done on input by the shell. Otherwise: * do variable, command, arithmetic substitutions * ignore escaped newlines * use \ to quote \, $, ', and first character of word
[n]«- word as above, but with leading tabs ignored
OF &> and >&, the first is preferred. It is equivalent, to >word 2>&1.


EXECUTION ORDER

All substitutions and I/O redirections are performed before a command is actually executed.

bash maintains an internal hash table for caching external commands. Initially, this table is empty. As commands are found by searching the directories listed in $PATH, they are added to the hash table.

The command search order is shell functions first, built-in commands second, and external commands (first in the internal hash table, and then via $PATH) third.


SIGNALS AND TRAPS

Signal handling is done with the trap built-in command. The word argument describing code to execute upon receipt of the signal is scanned twice by bash; oncce when the trap command is executed, and again when the signal is caught. There fore it is best to use sigle quotes for the trap command. Traps are executed in order of signal number. You cannot change the status of a signal that was ignored when the shell started up.

Traps on DEBUG happen after commands are executed.

Backgrounded commands (those followed by &) will ignore the SIGINT and SIGQUIT signals if the monitor option is turned off. Otherwise, they inherit the values of the parent bash.


CONDITIONAL EXPRESSIONS

Used with the ... compound command, which does not do pattern expansion or word splitting.

string : true if string is not NULL
-a file : true if file exists (-e is preferred)
-b file : true if file is a block device
-c file : true if file is a character device
-d file : true if file is a directory
-e file : true if file exists
-f file : true if file is a regular file
-g file : true if file has set gid bit set
-G file : true if file group is effective gid
-h file : true if file is a symbolic link
-k file : true if file has sticky bit set
-L file : true if file is a symbolic link
-n string : true if string has non-zero length
-N file : true if file exists and was modified since last read
-o option : true if option is on
-O file : true if file owner is effective uid
-p file : true if file is a fifo (named pipe)
-r file : true if file is readable
-s file : true if file has non-zero size
-S file : true if file is a socket
-t filedes : true if filedes is a terminal
-u file : true if file has set uid bit set
-w file : true if file is writable
-x file : true if file is executable
-z string : true if string has zero length
file1 -nt file2 : true if file1 is newer than file2 or file2 does not exist
file1 -ot file2 : true if file1 is older than file2 or file2 does not exist
file1 -ef file2 : true if file1 and file2 are the same file
string == pattern : true if string matches pattern
string != pattern : true if string does not match pattern
string1 < string2 : true if string1 is before string2
string1 > string2 : true if string1 is after string2
exp1 -eq exp2 : true if exp1 equals exp2
exp1 -ne exp2 : true if exp1 does not equal exp2
exp1 -lt exp2 : true if exp1 is less than exp2
exp1 -gt exp2 : true if exp1 is greater than exp2
exp1 -le exp2 : true if exp1 is less than or equal to exp2
exp1 -ge exp2 true if exp1 is greater than or equal to exp2
(expression) : true if expression is true, for grouping
!expression : true if expression is false
exp1 && exp2 : true if exp1 AND exp2 are true
exp1 || exp2 : true if exp1 or exp2 is true

If file is /dev/fd/n, then, if there is no /dev/fd directory, file descriptor n is checked. Otherwise, the real /dev/fd/n file is checked. Linux, FreeBSD, BSD/OS (and maybe others) return info for the indicated file descriptor, instead of the actual /dev/fd device file.

Both && and || are short circuit. Operands of comparison operators undergo arithmetic evaluation. For == and !=, quote any part of pattern to treat it as a string.