Show Menu
Cheatography

Bash expansions Cheat Sheet by

Quick reference for the "parameter expansions" and "conditional statements" sections of the bash manual

Official docs

Parameter expansion

${var:-word}
Substitute word if var is unset or null
${var:=word}
Substitute and assign word to var if var is unset/null
${var:?word}
Write word to stderr and exit if var is unset/null
${var:+word}
Substitute word if var is set or non null
${var:offse­t[:len]}
Expands to up to len characters of var starting at the character specified by offset.
${!var}
Indire­ction: expands the the value of the value of var
${!prefix*}
Expands to the names of variables whose names begin with prefix separated by IFS[0] (as single word)
${!prefix@}
Same as above but as multiple words
${!name[*]}
Expands to the list of array indices (keys) assigned in name
${#var}
Length of var, or length of array for ${#arr[*]}
${var#word}
Removes shortest prefix word from var.
${var##word}
Same as above but with longest prefix
${para­meter%word}
Removes shortest suffix word from var.
${para­meter%%word}
Same as above but with longest suffix
${var/pattern/string}
Substitute first instance of pattern in var with string
${var//pattern/string}
Same as above, but all instances
${var/#pattern/string}
Same as above, but pattern must be at beginning of var
${var/%pattern/string}
Same as above, but pattern must be at the end of var
${var^pattern}
Uppercases the first character of var if it matches pattern. Empty pattern matches all.
${var^^pattern}
Uppercases every match of pattern in var
${var,pattern}
Same as ${var^­pat­tern} but lowercase
${var,,pattern}
Same as ${var^­^pa­ttern} but lowercase
${var@­ope­rator}
Various operat­ions, see manual. Includes case modifi­cation, length, automatic quoting and escaping, variable export, reading attrib­utes.
3.5.3 Shell Parameter Expansion
word is subject to tilde expansion, parameter expansion, command substi­tution, and arithmetic expansion. pattern matches according to pattern matching.

Condit­ional Expres­sions

if
command
Checks for return code of
command
[ expression ]
External tool ("te­st") that runs with parameters expression and returns 0 for true, 1 for false.
Word splitting, parameter expansion and filename glob expansions occur before the test command runs: always quote variables to prevent word expansion and filename expansion unless desired.
[[ condit­ional ]]
Internal bash builtin that runs the condit­ional check. Expansions are performed except word splitting and filename expansion: quoting variables is not necessary.
Supports < and > as comparison operators, && and || as and/or chaining operators, =~ as regex matching operator

Order of expansions

1 - Word splitting
2 - Braces { }
3 - Tildes ~
4 - Parameters / variables ${ }
5 - Arithmetic $(( ))
6 - Commands $( )
7 - Word splitting (again)
8 - Pathnames ./*
9 - Process substi­tution <( )
Only brace expansion, word splitting, pathname expansion, "­$@" and "­${n­ame­[@]­}" can change the number of words of the expansion; other expansions expand a single word to a single word.
 

Command expansion

$(command)
Replaces the command substi­tution with the standard output of the command
`command`
Same as above but backslash retains its literal meaning unless escaped
If the substi­tution appears within double quotes, word splitting and pathname expansion are not performed on the results.

Process Substi­tution

<(p­rocess)
Substi­tutes with the standard output fd of process
>(p­rocess)
Substi­tutes with the standard input fd of process
Similar to piping but acts with files (named pipes), allowing them to work with commands that do not accept standard input unlike pipes, and allows working with multiple inputs­/ou­tputs without
... | tee

Arithmetic Expansion

$((exp­res­sion))
Evaluates expression as an arithmetic expression
Operator preced­ence, associ­ati­vity, and values are the same as in C. Shell variables can be used raw without $. Evaluation is done in fixed-­width integers. Prefixes indicate bases: "­0" for octal, "­0x" for hex, "­n#" for base n.

Pattern Matching

\
Escapes next character
*
Matches any string incl null string.
?
Matches any single char
[...]
As regex
?(patt­ern­-list)
Matches 0 or 1 instance of one of the patterns separated by |
*(patt­ern­-list)
Matches 0+ instances of one of the patterns separated by |
+(patt­ern­-list)
Matches 1+ instances of one of the patterns separated by |
@(patt­ern­-list)
Matches exactly 1 instance of one of the patterns separated by |
!(patt­ern­-list)
Matches anything except one of the patterns separated by |
**
When globstar is enabled, matches all files, dirs, subdirs and files in subdirs
"­Pattern list" patterns require the extglob option to be enabled.
Works in case statem­ents, == and != inside [[ ]], case modifi­cation, pattern match variable expansion and filename expansion if the "­pattern charac­ter­s" are not quoted.

I/O redire­ction

command [n]< file
Opens file for reading and redirects fd n (default 0, stdin) of command to file
command [n]> file
Opens file for writing and redirects fd n (default 1, stdout) of command to file
&>file
Redirects both stdin and stderr to file. Same as
>word 2>&1
&>­>file
Same as above but appends
<<word
text
word
Substitute standard input with text (can be multiple lines)
<<-word
text
word
As above but strips tabs from text and final word
<<"word"
text
word
As <<word but do not perform parameter expansion, command substi­tution, and arithmetic expansion in text
<<<word
Supplies word as a single line to fd n (default stdin)
[n]<&word
File descriptor n is made to be a copy of fd word for input
[n]>&word
Same as above but for output
[n]<&­digit-
Moves fd digit to fd n (default stdin) and closes digit
[n]>&­digit-
Same but for output (default stdout)
[n]<>word
Opens word for reading and writing on fd n
A fd number can be prepended to most forms in order to redirect to another fd instead of the default stdin/­out­/err.

Grouping Commands

(list)
Executes the commands in a subshell
{ list; }
Executes the commands in the current enviro­nment
The exit status of both of these constructs is the exit status of list. See pipefail option.
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Linux Command Line Cheat Sheet
          Bash/ZSH Shourtcuts Cheat Sheet
          bash Shortcuts Cheat Sheet

          More Cheat Sheets by armk

          PostgreSQL 101 for DBAs/sysadmins Cheat Sheet
          Postgresql settings/config quick reference Cheat Sheet