Home / Posts

Bash Reference Guide

tech bash

(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
fi

Conditional Operators

Here are common conditional operators that are used in condition checks

File Operators

These are operators used to check on files

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
fi

String Operators

These are operators used to perform checks on strings

Case Statement Syntax

case EXPRESSION in
    PATTERN1)
        COMMANDS1
        ;;
    PATTERN2)
        COMMANDS2
        ;;
    # ...and so on...
    *)
        DEFAULT_COMMAMDS
        ;;
esac

Loops

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
done

Or you can do range-based iteration:

for i {start_idx..end_idx[..increment]}; do
    # commands to execute
    echo "Current number: $i"
done

Note: C-style syntax also works

for (( i=start; i<=end; i++ )); do
    echo "Current number: $i"
done

while Loop

This loop runs commands until the condition is no longer met (no longer true)

while [ condition ]; do
    # Commmands to execute
done

Note: 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
done

Note: 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 world

Default 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 null

String 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 beginning

Remove Prefix

${parameter%pattern} # removes the shortest match of `pattern` from the end
${parameter%%pattern} # removes the longest match of `pattern` from the end

Replace 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 lowercase

Indirect Expansion

${!parameter} # uses the value of `parameter` as the name of another variable