Linux Shell Scripting Cheat Sheet

High-value Linux shell scripting reference covering variables, expansion, conditionals, loops, functions, redirection, pipelines, traps, getopts, and script debugging.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Variables and Expansion
Assign shell variable
name='alice'

# Set a shell variable in the current shell.

Export environment variable
export APP_ENV=production

# Expose a variable to child processes.

Use default value if unset
echo "${PORT:-8080}"

# Expand parameter with a fallback.

Require variable to be set
echo "${DATABASE_URL:?DATABASE_URL is required}"

# Fail fast if a variable is missing.

Capture command output
files=$(find . -type f | wc -l)

# Assign command output to a variable.

Quote variable safely
printf '%s
' "$name"

# Print variable preserving spaces.

## Conditionals Loops and Functions
Check whether file exists
if [[ -f /etc/app.conf ]]; then echo 'exists'; fi

# Use a shell test in an if statement.

Check whether directory exists
if [[ -d releases ]]; then ls releases; fi

# Test for directory presence.

Loop over items
for f in *.log; do echo "$f"; done

# Iterate through words or filenames.

Read file line by line
while IFS= read -r line; do echo "$line"; done < input.txt

# Process lines safely using while-read.

Branch with case
case "$1" in start) echo start ;; stop) echo stop ;; *) echo usage ;; esac

# Match input against multiple patterns.

Define shell function
greet() { echo "hello $1"; }

# Create a reusable shell function.

Enable strict shell behavior
set -euo pipefail

# Fail script on errors and unset vars.

Run cleanup on exit
trap 'rm -f "$tmpfile"' EXIT

# Register shell cleanup logic.

## Input Output and Pipelines
Redirect stdout to file
echo 'hello' > greeting.txt

# Write output to a file, overwriting it.

Append stdout to file
echo 'next line' >> app.log

# Append output to an existing file.

Redirect stderr to file
command 2> errors.log

# Send errors to a separate file.

Redirect stdout and stderr together
command > command.log 2>&1

# Capture all output in one file.

Pipe stdout to another command
ps aux | grep nginx

# Send output of one command as input to another.

Use process substitution
diff <(sort old.txt) <(sort new.txt)

# Pass command output as a pseudo-file.

Prompt user for input
read -rp 'Enter environment: ' env

# Read a value interactively.

Print formatted output
printf 'user=%s id=%s
' "$USER" "$UID"

# Use printf instead of echo for predictable formatting.

## Script Execution and Utilities
Use bash shebang
#!/usr/bin/env bash

# Start a Bash script with an explicit interpreter.

Make script executable
chmod +x deploy.sh

# Add execute bit to a script.

Run shell script
./deploy.sh

# Execute a script from the current directory.

Trace script execution
bash -x ./deploy.sh

# Print commands as they execute.

Parse short options with getopts
while getopts ':f:n:' opt; do case "$opt" in f) file="$OPTARG" ;; n) count="$OPTARG" ;; esac; done

# Handle flags in shell scripts.

Lint shell script
shellcheck deploy.sh

# Static-analyze shell scripts for bugs.