Bash scripting cheatsheet
Introduction
Example
Variables
Generally quote your variables unless they contain wildcards to expand or command fragments.
String quotes
Shell execution
Conditional execution
Functions
Conditionals
Strict mode
Brace expansion
Same as
Same as
Same as
Same as
Parameter expansions
Basics
Prefix name expansion
Indirection
Substitution
Remove suffix
Remove prefix
Remove long suffix
Remove long suffix
Remove long prefix
Remove long prefix
Replace first match
Replace all
Replace suffix
Replace prefix
Comments
Substrings
Substring (position, length)
Substring from the right
Length
Length of
Manipulation
Default values
, or if unset (or null)
Set to if unset (or null)
if is set (and not null)
Show error message and exit if is unset (or null)
Omitting the removes the (non)nullity checks, e.g. expands to if unset otherwise .
Loops
Basic for loop
C-like for loop
Ranges
With step size
Reading lines
Forever
Functions
Defining functions
Returning values
Raising errors
Arguments
Number of arguments
All positional arguments (as a single word)
All positional arguments (as separate strings)
First argument
Last argument of the previous command
Note: and must be quoted in order to perform as described. Otherwise, they do exactly the same
thing (arguments as separate strings).
Conditionals
Conditions
Note that is actually a command/program that returns either (true) or (false). Any program that
obeys the same logic (like all base utils, such as or ) can be used as condition, see
examples.
Empty string
Not empty string
Equal
Not Equal
Equal
Not equal
Less than
Less than or equal
Greater than
Greater than or equal
Regexp
Numeric conditions
More conditions
If OPTIONNAME is enabled
! EXPR Not
And
Or
File conditions
Exists
Readable
Symlink
Directory
Writable
Size is > 0 bytes
File
Executable
1 is more recent than 2
2 is more recent than 1
Same files
Example
$B
Arrays
Defining arrays
Working with arrays
Operations
Iteration
Dictionaries
Defining
Declares as a Dictionary object (aka associative array).
Working with dictionaries
Iteration
Iterate over values
Iterate over keys
Options
Options
Glob options
Set as a colon-separated list of patterns to be removed from glob matches.
History
Commands
Show history
Don’t execute expanded result immediately
Expansions
Expand last parameter of most recent command
Expand all parameters of most recent command
Expand th most recent command
Expand th command in history
Expand most recent invocation of command
Operations
Execute last command again
Replace first occurrence of to in most recent command
Replace all occurrences of to in most recent command
Expand only basename from last parameter of most recent command
Expand only directory from last parameter of most recent command
and can be replaced with any valid expansion.
Slices
Expand only th token from most recent command (command is ; first argument is )
Expand first argument from most recent command
Expand last token from most recent command
Expand range of tokens from most recent command
Expand th token to last from most recent command
can be replaced with any valid expansion i.e. , , , etc.
Miscellaneous
Numeric calculations
Subshells
Redirection
Inspecting commands Trap errors
or
"ERROR:
Case/switch
Source relative
printf
Transform strings
Operations apply to characters not in the given set
Delete characters
Replaces repeated characters with single occurrence
Truncates
All upper case letters
All lower case letters
All digits
All whitespace
All letters
All letters and digits
Example
Directory of script
Getting options
Heredoc
Reading input
The option disables a peculiar legacy behavior with backslashes.
Special variables
Exit status of last task
PID of last background task
PID of shell
Filename of the shell script
Last argument of the previous command
return value of piped commands (array)
s.
Go to previous directory
Check for command’s result
Grep check