Shell Scripting — Cheat Sheet

Tags: bash, shell, linux, scripting, automation, cli

Level: intermediate

TL;DR: Essential concepts, commands, and interview answers for Shell Scripting (Bash) to quickly review variables, control structures, and common gotchas.

Key concepts

-

Shebang (#!/bin/bash) tells the OS which interpreter to use for the script.

-

Variables do not have spaces around the = sign (e.g., VAR="value").

-

$1, $2, etc. represent positional parameters passed to the script; $# is the number of arguments.

-

$? holds the exit status of the last executed command (0 for success, non-zero for failure).

-

&& (AND) and || (OR) are used to chain commands based on their exit status.

-

File testing operators (e.g., -e for exists, -d for directory, -f for regular file) are commonly used in if statements.

-

stdout (1) and stderr (2) can be redirected using >, >>, 2>, and &>.

-

Pipes (|) take the standard output of one command and pass it as standard input to another.

-

Backticks `command or $() are used for command substitution, with $()` being the modern preferred syntax.

-

Variables should be double-quoted ("$VAR") to prevent word splitting and globbing issues.

Typical questions and ready answers

-

Q: What is the difference between single quotes and double quotes in Bash?R: I use double quotes to allow variable expansion and command substitution, while single quotes treat every character literally, preventing any expansion.

-

Q: How do you run a script in the background?R: I add an ampersand (&) at the end of the command. To keep it running after closing the terminal, I use nohup or disown.

-

Q: What does set -e do in a script?R: I use set -e to make the script exit immediately if any command returns a non-zero exit status, which prevents errors from cascading.

-

Q: How do you iterate over all files in a directory?R: I typically use a for loop like for file in /path/to/dir/*; do ... done rather than parsing ls output.

Pitfalls and common mistakes

-

Parsing ls output instead of using globbing (e.g., for f in $(ls *.txt)), which breaks on filenames with spaces.

-

Forgetting to quote variables (e.g., if [ $VAR == "test" ] instead of if [ "$VAR" == "test" ]), leading to syntax errors if the variable is empty or contains spaces.

-

Using == instead of = for string comparison in [ (POSIX test). Double equals is valid in [[, but = is standard in [.

-

Leaving spaces around the assignment operator (e.g., VAR = "value" will try to execute VAR as a command).

-

Not providing a shebang at the very top of the script, leading to unexpected interpreter usage.

Snippets and commands

#!/bin/bash
# Enable strict mode
set -euo pipefail

# Check if an argument is provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 <filename>"
    exit 1
fi

FILE="$1"

# Check if file exists
if [ -f "$FILE" ]; then
    echo "File $FILE exists."
else
    echo "File not found!"
    exit 2
fi

# Iterate over a list of items
for i in {1..5}; do
    echo "Iteration $i"
done

# Read file line by line
while IFS= read -r line; do
    echo "Line: $line"
done < "$FILE"

To review

-

[ ] awk and sed basics for text processing.

-

[ ] Difference between [ (test command) and [[ (bash keyword).

-

[ ] Parameter expansion tricks like ${VAR:-default} or ${VAR%pattern}.

Jarvis Interviews
Jarvis