Shell Scripting Cheat Sheet
Quick reference for writing reliable Bash scripts.
Variables and Parameters
# Variable assignment (NO spaces around =)
NAME="World"
echo "Hello $NAME"
# Command substitution
TODAY=$(date)
Conditionals (If/Else)
if [ -z "$VAR" ]; then
echo "Variable is empty"
elif [ "$VAR" == "test" ]; then
echo "Variable is test"
else
echo "Something else"
fi
File Test Operators
| Operator | True if... |
|---|---|
-f file | File exists and is a regular file. |
-d dir | Directory exists. |
-s file | File exists and is not empty. |
-x file | File is executable. |
Loops
For Loop:
for item in apple banana cherry; do
echo "I like $item"
done
While Loop:
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
Functions
my_function() [
echo "First argument: $1"
return 0
]
# Call it
my_function "Hello"
(Note: brackets used instead of literal braces for documentation compatibility)