(Published: 2025-08-30)
Here is a compendium of reference information regarding
bash (also applies to zsh) for syntatic
sugar that I keep finding myself googling. I’d much rather keep
these handy with me offline to make sure I’m always prepared for
quick shell scripting or issuing complex commands on the go.
if-else Statements
The typical if-else statement in bash
looks like this:
if [ condition1 ]; then
# commands to execute if condition1 is met
elif [ condition2 ]; then
# commands to execute if condition2 is met
else
# commands to execute if none of above conditions are met
fiConditional Operators
Here are common conditional operators that are used in condition checks
File Operators
These are operators used to check on files
-e: checks if file exists-f: checks if regular file-d: checks if argument is a directory-s: checks if file is non-empty-r: checks if file is readable-w: checks if file is writable-x: checks if file is executable
Keep in mind that these operators are not stackable. If you want to check if a given file is both readable and writable, you would do something like this:
if [ -r "filename.txt" ] && [ -w "filename.txt ]; then
# file is both readable and writable
fiString Operators
These are operators used to perform checks on strings
string: true if the length ofstringif non-zero==/=: true if both string operands are equal!=: true if the string operands are not equal<: true ifstring1sorts beforestring2lexographically>: true ifstring1sorts afterstring2lexographically-eq,-ne,-lt,-le,-gt,-ge: string arithmetic checks
Case Statement Syntax
case EXPRESSION in
PATTERN1)
COMMANDS1
;;
PATTERN2)
COMMANDS2
;;
# ...and so on...
*)
DEFAULT_COMMAMDS
;;
esacLoops
There are multiple types of loops in bash scripting depending on the use case
for Loop
You can do list-based iteration like so:
for variable in item1 item2 item3; do
# commands to execute for each item
echo "Reference item with: $variable
doneOr you can do range-based iteration:
for i {start_idx..end_idx[..increment]}; do
# commands to execute
echo "Current number: $i"
doneNote: C-style syntax also works
for (( i=start; i<=end; i++ )); do
echo "Current number: $i"
donewhile Loop
This loop runs commands until the condition is no longer met (no longer true)
while [ condition ]; do
# Commmands to execute
doneNote: You can use the same conditional operators
as seen in the if-else section
until Loop
This loop runs in the inverse logic of the while
loop. It runs until the condition is met (becomes
true).
until [ condition ]; do
# Commands to execute
doneNote: You can use the same conditional operators
as seen in the if-else section
Parameter Expansion
When invoking parameters and variables in shell scripting, you may need to transform or manipulate the content in some ways:
Basic Expansion
Simple substitution ivolves using $paramter or
better yet ${parameter} to substitute the content of
the variable in place.
myvar="hello world"
echo "${myvar}" # output: hello worldDefault Value
You can give a default value for safety if a parameter/variable may be empty
${paramater:-default} # uses "default" if `parameter` is unset or null
$(parameter:=default} # same as above but assigns "default" to `parameter`
${parameter:?default} # displays "default" as an error message and exits if unset
$(parameter:+default} # uses "default" if `parameter` is set and not nullString Manipulation
You can use the following categories of manipulation on strings to manipulate and transform the output if you want a subset of the string only
Substring Manipulation
${parameter:offset} # extracts substring from `offset` (int)
${parameter:offset:length} # extracts substring of length `length` from `offset` (int)Remove Prefix
${parameter#pattern} # removes the shortest match of `pattern` from the beginning
${parameter##pattern} # removes the longest match of `pattern` from the beginningRemove Prefix
${parameter%pattern} # removes the shortest match of `pattern` from the end
${parameter%%pattern} # removes the longest match of `pattern` from the endReplace Substring
${parameter/pattern/string} # replaces the first match of `pattern` with `string`
${parameter//pattern/string} # replaces all matches of `pattern` with `string`Case Modification
${parameter^pattern} # converts the first character of the matched pattern to uppercase
${parameter^^pattern} # converts all characters of the matched pattern to uppercase
${parameter,pattern} # converts the first character of the matched pattern to lowercase
${parameter,,pattern} # converts all characters of the matched pattern to lowercaseIndirect Expansion
${!parameter} # uses the value of `parameter` as the name of another variable