Every Bash script starts with a shebang line, followed by commands.
#!/bin/bash
VAR="world"
echo "Hello $VAR!" # => Hello world!
A comprehensive cheat sheet for Bash scripting, covering essential syntax, commands, and best practices.
Every Bash script starts with a shebang line, followed by commands.
|
To execute the script:
|
Defining variables: |
|
Accessing variables: |
|
Important Note: |
|
Inline comments:
|
Multi-line comments:
|
Defining arrays:
|
Indexing:
|
|
Array as arguments
|
${FOO%suffix} |
Remove suffix |
${FOO#prefix} |
Remove prefix |
${FOO%%suffix} |
Remove long suffix |
${FOO##prefix} |
Remove long prefix |
${FOO/from/to} |
Replace first match |
${FOO//from/to} |
Replace all |
${FOO/%from/to} |
Replace suffix |
${FOO/#from/to} |
Replace prefix |
|
Initialize an empty array. |
|
Initialize an array with values. |
|
Assign value to array element at index 0. |
|
Append “Watermelon” to the |
|
Another way to append “Watermelon” to the |
|
Remove elements matching the regex |
|
Remove the element at index 2 from the |
|
Duplicate the |
|
Concatenate the |
|
Read lines from “logfile” into the |
|
Get the number of elements in the |
Defining a function |
|
Calling a function |
|
Returning values from a function |
|
Returning status codes |
|
Local variables |
Use
|
Passing arguments |
Arguments are accessed via
|
Accessing all arguments |
|
Number of arguments |
|
Shift arguments |
The
|
Function scope |
Functions are executed in the same shell environment as the caller. Variables declared outside the function are accessible inside unless shadowed by a |
Recursion |
Bash functions can be recursive, but recursion depth is limited.
|
Unsetting a function |
Use
|
Using function libraries |
Source a file containing function definitions to make them available in your script.
|
$1 … $9 |
Parameter 1 … 9 |
$0 |
Name of the script itself |
$1 |
First argument |
${10} |
Positional parameter 10 |
$# |
Number of arguments |
$$ |
Process id of the shell |
$* |
All arguments |
$@ |
All arguments, starting from first |
$- |
Current options |
$_ |
Last argument of the previous command |
Integer conditions: |
|
String conditions: |
|
File conditions: |
|
Basic |
Iterates over a list of items.
|
C-like |
Uses arithmetic expressions for iteration.
|
Ranges in |
Iterates over a sequence of numbers.
|
|
Specifies the increment value.
|
Auto increment in |
Increments a variable in each iteration.
|
Auto decrement in |
Decrements a variable in each iteration.
|
|
Skips the current iteration and proceeds to the next.
|
|
Exits the loop entirely.
|
|
Executes a block of code until a condition is true.
|
Infinite |
Loops indefinitely.
Or shorthand:
|
Reading lines from a file |
Iterates through each line of a file.
|
Nested Loops |
Using loops inside another loop.
|
Looping through arrays |
Iterating through elements of an array.
|
Using |
Creates a menu for user selection.
|
Declare an associative array using
|
Assign key-value pairs individually:
|
Assign multiple key-value pairs at once:
|
Example:
|
Another Example:
|
Access a value by its key:
|
If the key doesn’t exist, an empty string is returned.
|
Example:
|
Using default values if a key doesn’t exist:
|
Get all values:
|
Get all keys:
|
Example:
|
When using |
Get the number of elements in the dictionary:
|
Check if a key exists:
|
Example:
|
Delete a specific key-value pair:
|
Delete the entire dictionary:
|
Example:
|
Check if the dictionary is empty after deletion:
|
Iterate through values:
|
Iterate through keys:
|
Iterate through both keys and values:
|
Example:
|
Pass a dictionary to a function:
|
Return a dictionary from a function (less common, usually modify in place using nameref):
|
Dictionaries can be used to store configuration settings:
|
Simulate data structures:
|
Loading configurations from a file:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Changes to the directory within the subshell do not affect the current shell’s working directory. Example:
|
Example:
|
Using subshells for parallel execution (using Example:
|
This will tell you if Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
Basic
|
Example using
|
Using
|
Example:
|
Using
|
Example with line number reporting:
|
Using a function to handle errors:
|
Example:
|
Example:
|
Example:
|
Example:
|
Example:
|
`$?` |
Exit status of the last executed command. Example:
|
`$!` |
Process ID (PID) of the last background command. Example:
|
`$$` |
PID of the current shell. Example:
|
`$0` |
Filename of the current shell script. Example:
|
`$#` |
Number of arguments passed to the script. Example:
|
`$@` |
All arguments passed to the script (as separate words). Example:
|
`$*` |
All arguments passed to the script (as a single word). Example:
|