## 1 Bash References

“\a”
“\b”
backspace
“\e”
escape character for ANSI
“\E”
an escape character (not ANSI C)
“\f”
form feed
“\n”
newline
“\r”
carriage return
“\t”
horizontal tab
“\v”
vertical tab
“\\”
backslash
“\’”
single quote
“\”“
double quote
“\?”
question mark
“\nnn”
the eight-bit character whose value is the octal value
(one to three octal digits)
“\xHH”
the eight-bit character whose value is the hexadecimal
value HH (one or two hex digits)
“\uHHHH”
the Unicode (ISO/IEC 10646) character whose value is the
hexadecimal value HHHH (one to four hex digits)
“\UHHHHHHHH”
the Unicode (ISO/IEC 10646) character whose value is
the hexadecimal value HHHHHHHH (one to eight hex digits)
“\cx”
a control-x character

## 2 Reserved Words

if	then	elif	else	fi	time
for	in	until	while	do	done
case	esac	coproc	select	function
{	}	[[	]]      !


## 3 Pipeline

A pipeline is a sequence of one or more commands separated by one of
the control operators | or |&. The format for a pipeline is

• [time [-p]] [!] command1 [ | or |& command2 ] …

If |& is used, command1’s standard error, in addition to its
standard output, is connected to command2’s standard input through
the pipe; it is shorthand for 2>&1 |.

This implicit redirection of the standard error to the standard
output is performed after any redirections specified by the
command.

## 4 Command Sequence

A list is a sequence of one or more pipelines separated by one of the
operators ;, &, &&, or ||, and optionally terminated by one of
;, &, or a newline.

Of these list operators, && and || have equal precedence, followed
by ; and &, which have equal precedence.

A sequence of one or more newlines may appear in a list to delimit
commands, equivalent to a semicolon.

An AND list has the form

• command1 && command2

command2 is executed if, and only if,
command1 returns an exit status of zero (success).

An OR list has the form

• command1 || command2

command2 is executed if, and only if,
command1 returns a non-zero exit status.

The return status of AND and OR lists is the exit status of the
last command executed in the list.

## 5 Loops

Note that wherever a ; appears in the description of a command’s
syntax, it may be replaced with one or more newlines.

until

The syntax of the until command

• until test-commands; do consequent-commands; done

Execute consequent-commands as long as test-commands has an exit
status which is not zero. The return status is the exit status of
the last command executed in consequent-commands, or zero if none
was executed.

while

The syntax of the while command is:

• while test-commands; do consequent-commands; done

Execute consequent-commands as long as test-commands has an exit
status of zero. The return status is the exit status of the last
command executed in consequent-commands, or zero if none was
executed.

for

The syntax of the for command is:

• for name [ [in [words …] ] ; ] do commands; done
• for (( expr1 ; expr2 ; expr3 )) ; do commands ; done

The return status is the exit status of the last command that
executes. If there are no items in the expansion of words, no
commands are executed, and the return status is zero.

## 6 Conditionals

### 6.1 If Conditional

if test-commands; then
echo "consequent-commands ...";
elif more-test-commands; then
echo "more-consequents ...";
else
echo "alternate-consequents ...";
fi


The test-commands list is executed, and if its return status is zero,
the consequent-commands list is executed. If test-commands returns a
non-zero status, each elif list is executed in turn, and if its exit
status is zero, the corresponding more-consequents is executed and the
command completes. If ‘else alternate-consequents’ is present, and the
final command in the final if or elif clause has a non-zero exit
status, then alternate-consequents is executed. The return status is
the exit status of the last command executed, or zero if no condition
tested true.

### 6.2 Case Conditional

case "b" in
"a") echo "A!";;
"b") echo "B!";; #==> B!w
"c") echo "C!";;
\?) echo "idk?"
esac


There may be an arbitrary number of case clauses, each terminated by a
;;, ;&, or ;;&. The first pattern that matches determines the
command-list that is executed. It´s a common idiom to use * as the
final pattern to define the default case, since that pattern will
always match.

echo -n "Enter the name of an animal: "
echo -n "The $ANIMAL has " case$ANIMAL in
horse | dog | cat) echo -n "four";;
man | kangaroo ) echo -n "two";;
*) echo -n "an unknown number of";;
esac
echo " legs."


### 6.3 Select

The select construct allows the easy generation of menus. It has
almost the same syntax as the for command:

• select name [in words …]; do commands; done

The list of words following in is expanded, generating a list of
items. The set of expanded words is printed on the standard error
output stream, each preceded by a number. If the ‘in words’ is
omitted, the positional parameters are printed, as if ‘in “$@”’ had been specified. The PS3 prompt is then displayed and a line is read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the select command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The commands are executed after each selection until a break command is executed, at which point the select command completes. Here is an example that allows the user to pick a filename from the current directory, and displays the name and index of the file selected. select fname in *; do echo you picked$fname $$REPLY$$
break;
done


## 7 Lists

• (list) Creates a subshell env, where each command in list are
sequentially executed.
• {list,...} Does not create a subshell env, where each command in list
are sequentially executed.

## 8 Paralell

Finish first outputs first

{
echo foss.org.my ;
echo debian.org ;
echo freenetproject.org ;
} | parallel traceroute


Execute in order in paralell

{
echo foss.org.my ;
echo debian.org ;
echo freenetproject.org ;
} | parallel -k traceroute


## 9 Functions

let func2, var = func1 local

func1()
{
local var='func1 local'
func2
}

func2()
{
echo "In func2, var = $var" } var=global func1  ## 10 brace expansion bash$ echo a{d,c,b}e


Common usage:

mkdir /usr/local/src/bash/{old,new,dist,bugs}

chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}


## 11 Tilde Expansion

The following table shows how Bash treats unquoted tilde-prefixes:

~

#    The value of $HOME ~/foo #$HOME/foo
~fred/foo

#    The subdirectory foo of the home directory of the user fred
~+/foo

#    $PWD/foo ~-/foo #${OLDPWD-'~-'}/foo
~N

#    The string that would be displayed by ‘dirs +N’
~+N

#    The string that would be displayed by ‘dirs +N’
~-N

#    The string that would be displayed by ‘dirs -N’


## 12 Parameter Expansion

• ${parameter:-word} If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. • ${parameter:=word}

If parameter is unset or null, the expansion of word is assigned
to parameter. The value of parameter is then
substituted. Positional parameters and special parameters may not
be assigned to in this way.

• ${parameter:?word} If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted. • ${parameter:+word}

If parameter is null or unset, nothing is substituted, otherwise
the expansion of word is substituted.

• ${parameter:offset} • ${parameter:offset:length}

This is referred to as Substring Expansion. It expands to up to
length characters of the value of parameter starting at the
character specified by offset. If parameter is ‘@’, an indexed
array subscripted by ‘@’ or ‘*’, or an associative array name, the
results differ as described below. If length is omitted, it
expands to the substring of the value of parameter starting at the
character specified by offset and extending to the end of the
value. length and offset are arithmetic expressions (see Shell
Arithmetic).

If offset evaluates to a number less than zero, the value is used
as an offset in characters from the end of the value of
parameter. If length evaluates to a number less than zero, it is
interpreted as an offset in characters from the end of the value
of parameter rather than a number of characters, and the expansion
is the characters between offset and that result. Note that a
negative offset must be separated from the colon by at least one
space to avoid being confused with the ‘:-’ expansion.

Here are some examples illustrating substring expansion on parameters
and subscripted arrays:

$string=01234567890abcdefgh$ echo ${string:7} 7890abcdefgh$ echo ${string:7:0}$ echo ${string:7:2} 78$ echo ${string:7:-2} 7890abcdef$ echo ${string: -7} bcdefgh$ echo ${string: -7:0}$ echo ${string: -7:2} bc$ echo ${string: -7:-2} bcdef$ set -- 01234567890abcdefgh
$echo${1:7}
7890abcdefgh
$echo${1:7:0}

$echo${1:7:2}
78
$echo${1:7:-2}
7890abcdef
$echo${1: -7}
bcdefgh
$echo${1: -7:0}

$echo${1: -7:2}
bc
$echo${1: -7:-2}
bcdef
$array[0]=01234567890abcdefgh$ echo ${array[0]:7} 7890abcdefgh$ echo ${array[0]:7:0}$ echo ${array[0]:7:2} 78$ echo ${array[0]:7:-2} 7890abcdef$ echo ${array[0]: -7} bcdefgh$ echo ${array[0]: -7:0}$ echo ${array[0]: -7:2} bc$ echo ${array[0]: -7:-2} bcdef  If parameter is ‘@’, the result is length positional parameters beginning at offset. A negative offset is taken relative to one greater than the greatest positional parameter, so an offset of -1 evaluates to the last positional parameter. It is an expansion error if length evaluates to a number less than zero. The following examples illustrate substring expansion using positional parameters: $ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$echo${@:7}
7 8 9 0 a b c d e f g h
$echo${@:7:0}

$echo${@:7:2}
7 8
$echo${@:7:-2}
bash: -2: substring expression < 0
$echo${@: -7:2}
b c
$echo${@:0}
./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$echo${@:0:2}
./bash 1
$echo${@: -7:0}


If parameter is an indexed array name subscripted by ‘@’ or ‘*’, the
result is the length members of the array beginning with
${parameter[offset]}. A negative offset is taken relative to one greater than the maximum index of the specified array. It is an expansion error if length evaluates to a number less than zero. These examples show how you can use substring expansion with indexed arrays: $ array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h)
$echo${array[@]:7}
7 8 9 0 a b c d e f g h
$echo${array[@]:7:2}
7 8
$echo${array[@]: -7:2}
b c
$echo${array[@]: -7:-2}
bash: -2: substring expression < 0
$echo${array[@]:0}
0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$echo${array[@]:0:2}
0 1
$echo${array[@]: -7:0}



## 13 Command Substutution

Command substitution allows the output of a command to replace the
command itself. Command substitution occurs when a command is enclosed
as follows:

• $(command) • command Bash performs the expansion by executing command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the

## 16 Return values and subprocesses

Each command in a pipeline is executed in its own subshell, which is a
separate process (see Command Execution Environment). If the lastpipe
option is enabled using the shopt builtin (see The Shopt Builtin), the
last element of a pipeline may be run by the shell process.

The exit status of a pipeline is the exit status of the last command
in the pipeline, unless the pipefail option is enabled (see The Set
Builtin).

If pipefail is enabled, the pipeline=s return status is the value of
the last (rightmost) command to exit with a non-zero status, or zero
if all commands exit successfully. If the reserved word ! precedes
the pipeline, the exit status is the logical negation of the exit
status as described above. The shell waits for all commands in the
pipeline to terminate before returning a value.