Bash

Bash is a Unix shell and command language.

See the GNU Bash Manual for descriptions of the features present in the Bash shell.

Scripting

A list of commands to be executed sequentially can be combined into a single file called a script.

Run a shell script with the sh command followed by the file path.

Adding a shebang to the beginning of a script and making it an executable by running chmod +x <file_name> allows the file to be called without the sh command. chmod -x reverts the executable to a plain file.

Shebang

Scripts begin with a shebang #! followed by the absolute path to the interpreter /bin/<path_to_interpreter>. You can also modify the shebang to ensure that the script is portable across systems by using /usr/bin/env <program>. Read more about script portability.

Quote delimiters

Delimiter Description
' Prevent variable interpolation
" Allow variable interpolation
` Reserved for command substitution

Variables

foo="bar"
echo $foo
# prints bar

Bash special variables

Variable Description
$0 Name of the script
$1 Arguments to the script (e.g. $1, $2 ... $n)
$@ All arguments
$# Number of arguments
$? Return code of the previous command
$$ Process identification number (PID) for the current script
!! Entire last command, including arguments.
$_ Last argument from the last command.

Operators

Arithmetic comparison operators

Operators in the left column work with single brackets [ ] or double brackets [[ ]]; operators in the right column only work with double brackets.

POSIX Bash Description
-eq == Equal
-ne != Not equal
-lt < Less than
-gt > Greater than
-le <= Less than or equal
-ge >= Greater than or equal

Arithmetic operators

Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulus
** Power

String comparison operators

Operator Description
= Equal
!= Not equal
< Less than
> Greater than
-n Is not empty
-z Is empty

Boolean operators

Operator Description
&& And
|| Or
! Not

File testing operators

See a full list by running man test.

Control flow

if/else

if statements check the exit code of commands. $? returns the exit code of the most recently run command. An if statement with the condition wrapped in square brackets checks if the condition renders true.

if [ expression ]
then
  # Statement(s) to be executed if condition is true
else
  # Statement(s) to be executed if condition is false
fi

Conditional expressions

Conditional expressions are used by the [[ compound command and the test and [ builtin commands. Expressions may be unary or binary. A few useful expressions:

Expression Description
-a file True if file exists
-d file True if file exists and is a directory
-e file True if file exists
-f file True if file exists and is a regular file

List of Bash conditional expressions

Substitutions

Command substitution

Command substitution is the mechanism by which the shell performs a given set of commands and then substitutes their output in the place of the commands.

Syntax:

# Older shells
`command`
# Modern shells
$(command)

Variable substitution

Variable substitution enables the shell programmer to manipulate the value of a variable based on its state.

# Substitute the value of var
${var}

More at https://www.tutorialspoint.com/unix/unix-shell-substitutions.htm

Process substitution

Process substitution allows a process’s input or output to be referred to using a filename.

# Output
<(command)

# Input
>(command)

# Example
cat <(ls)

Functions

name_of_func() {
  # first param is `$1`
  # second param is `$2`
  # etc.
}

# call the function
name_of_func "var_1_param" "var_2_param"

More at https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html

Globbing

Wildcard Description
* Matches zero or more characters
? Matches one character
! Excludes characters
[] Matches one character between the brackets
a-z 0-9 Matches range of characters between the brackets

Shell expansions

Brace expansion

Brace expansion is a mechanism by which arbitrary strings may be generated. Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript. The preamble is prefixed to each string contained within the braces, and the postscript is then appended to each resulting string, expanding left to right.

echo a{d,c,b}e
ade ace abe

Resources