Bash shell Cheatsheet
Sample program
#!/usr/bin/env bash
echo 'Hello from Bash!'
Comments
Declare the comment:
# my comment
I/O commands
Print the string with a trailing \n
:
echo 'Hello from Bash!'
# or
printf '%s\n' 'Hello from Bash!'
Read the string to a variable:
read my_variable
Variables
Defining and erasing
Declare the global/local variable:
declare my_variable='Hello from Bash!'
local my_variable='Hello from Bash!'
Remove the variable:
unset my_variable
Define Variables using export
export my_variable='Hello from Bash!'
Remove variable defined by export
export -n my_variable
Slicing
Slice the variable:
echo ${my_variable:1:10}
echo ${my_variable:2}
echo ${my_variable:0:-2}
Arithmetic
Incrementing and decrementing
Increment/decrement the variable:
((my_variable++))
((my_variable--))
Integer manipulations
Calculate the number sum:
echo $((1 + 2))
Operator | Performs |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
** | Exponentiation |
Float manipulations
Calculate the number sum:
awk 'BEGIN { print 1.0 + 2.0 }'
Operator | Performs |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
** | Exponentiation |
String manipulation
Matching
Match the string against a regular expresion:
[[ 'Hello from Bash!' =~ 'Bash' ]]
Extended regular expressions described.
Pattern | Matches |
---|---|
x? | Zero or one x chars |
x* | Any count x chars |
x+ | One or more x chars |
x{n} | n times x chars |
x{n,m} | n to m times x chars |
x{n,} | n or more times x chars |
[xy] | x or y char |
[^xy] | not x or y char |
Modifying
Remove the shortest/longest matching pattern from beginning:
echo ${my_variable#*=}
echo ${my_variable##*=}
Remove the shortest/longest matching pattern from ending:
echo ${my_variable%*=}
echo ${my_variable%%*=}
Replace the first/all matching pattern:
echo ${my_variable/Bash/bash}
echo ${my_variable//Bash/bash}
Conditionals
Compare two variables:
if [[ $my_variable -lt $another_variable ]]; then
···
elif [[ $my_variable -eq $another_variable ]]; then
···
else
···
fi
Integer operator | Meaning |
---|---|
-lt | [L]ess [t]han |
-eq | [Eq]ual |
-gt | [G]reater [t]han |
-le | [L]ess than or [e]qual to |
-ge | [G]reater than or [e]qual to |
-ne | [N]ot [E]qual |
String operator | Meaning |
---|---|
== | [Eq]ual |
!= | [N]ot [E]qual |
File operator | Meaning |
---|---|
-f | [F]ile exists |
-d | [D]irectory exists |
-r | File or directory exists and [r]eadable |
-w | File or directory exists and [w]ritable |
-x | File or directory exists and e[x]ecutable |
Loops
Iterate over the number range:
for i in {1,10}; do
...
done
Process communication
Files
Write the string with a trailing \n
to a file:
echo 'Hello from Bash!' > my_file
Write (append) the string with a trailing \n
to a file:
echo 'Hello from Bash!' >> my_file
Piping
Pass the first command stdout output as an input to a second command:
my_command | another_command
Command substitution
Replace the command invocation with its stdout output:
echo $(expr $my_variable + 1)
Process substitution
Replace the command invocation with a temporary file name with a command stdout output:
echo <(expr $my_variable + 1)
Functions
Defining and erasing
Declare the function:
my_function() {
···
}
Remove the function:
unset my_function
Arrays
Defining arrays:
Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"
Iteration:
for i in "${arrayName[@]}"; do
echo $i
done