Bash script review: quoting, failures and destructive paths
Review Bash quoting, destructive paths, exit statuses and dependencies with concrete examples, exact finding references and static-analysis limitations.
A useful Bash review starts with arguments and side effects, not a blanket instruction to enable strict mode. A quoted path can still be the wrong path; a command can fail without stopping its caller; an apparently harmless expansion can supply several arguments. Read the script in order and identify which values cross a trust boundary before choosing a fix.
Keep data separate from arguments
In ordinary Bash commands, an unquoted parameter expansion can undergo word splitting and filename expansion. cp $source $target therefore does not necessarily receive two path arguments. Quote each scalar and use "$@" for the original argument list. For a collection of options, an array preserves element boundaries more reliably than a space-separated string. Assignment and [[ tests have different expansion rules, so evaluate the actual context rather than reporting every dollar sign.
Guard the operation, not just the variable
Before recursive removal, establish that a destination belongs to the expected directory and is not empty. The ${dir:?message} expansion rejects unset or empty values, but does not prove that an existing value is safe. Quoting prevents splitting, not deletion of an unintended directory. If a preceding cd fails, an explicit cd "$dir" || exit 1 protects subsequent relative operations. Read symbolic links and concurrent changes as unknown unless their handling appears in the script.
Review failure paths explicitly
set -e is context-sensitive: commands used in conditions and parts of && or || lists do not behave like unconditional commands. set -u changes how optional arguments must be read, and pipefail changes pipeline status. These options can help, but they are not a substitute for handling an expected grep miss or a failed deployment step. Check the status that the caller actually receives and whether cleanup or rollback is attempted after a failure.
Inspect downloads, temporary state and dependencies
curl piped to bash places remote text directly at an execution boundary. Static review can observe that boundary, but cannot verify the downloaded content. A separate download with integrity verification is easier to audit. Predictable temporary paths require extra care on shared machines; mktemp reduces collisions but cleanup still needs an exit path. List commands actually referenced, and do not assume that their versions, permissions or availability match your laptop.
Review example
This deliberately unsafe sample illustrates argument splitting, unchecked directory changes and an unguarded removal path. It is review material, not a deployment recipe.
#!/usr/bin/env bash
source_path=${1:-}
target_path=${2:-}
cp $source_path $target_path
cd /var/app
rm -rf $BUILD_DIR/*
Open the checkerWhat cannot be verified
The checker reads text without executing commands. It cannot prove a backup exists, a path points to the intended directory, a dependency is installed or a server accepted a request. Treat severity as review prioritization and inspect the evidence for each claim. Use the exact rule sections below to decide whether a warning applies before making a minimal change.
Finding references
rm -rf with a variable path
Recursive/forced rm using a variable path. If the variable is empty or unset, this can delete far more than intended.
Suggested safeguard: Guard the path: use "${dir:?dir is required}" and quote the expansion, e.g. rm -rf "${dir:?}/subdir".
SC2115Piping a download into a shell
Remote content is piped straight into a shell. A compromised or partially downloaded response executes with your privileges.
Suggested safeguard: Download to a file, verify it (checksum or signature), inspect it, then execute it as a separate step.
eval with an expansion
eval is used with an expansion. Any attacker-controlled part of that string becomes executable code.
Suggested safeguard: Replace eval with an array, a case statement, or indirect expansion (${!name}) — none of which re-parse data as code.
SC2294World-writable chmod 777
chmod 777 makes the target world-writable, so any local account can modify or replace it.
Suggested safeguard: Grant the narrowest mode that works — 755 for executables, 644 for data, 700 for private state.
cd without a failure guard
cd without a failure guard. If the directory does not exist the script keeps running in the wrong working directory.
Suggested safeguard: Write cd "$dir" || exit 1 (or set -e plus a quoted path) so the script stops instead of operating on the wrong tree.
SC2164Unquoted variable expansion
Unquoted variable expansion. Values containing spaces or glob characters get word-split and globbed before the command sees them.
Suggested safeguard: Wrap the expansion in double quotes: "$var" / "${arr[@]}".
SC2086Legacy backtick substitution
Legacy backtick command substitution. It nests badly and handles backslashes differently from the modern form.
Suggested safeguard: Use $(command) instead of `command`.
SC2006Single-bracket test in Bash
Single-bracket [ ] test. It word-splits its operands, so an empty or spaced variable produces a syntax error at runtime.
Suggested safeguard: In Bash scripts prefer [[ ... ]], which does not word-split or glob its operands.
Non-portable echo -e
echo -e is not portable — it is undefined in POSIX sh and behaves differently across shells and /bin/echo builds.
Suggested safeguard: Use printf '%b\n' "$value" for escape handling that behaves the same everywhere.
SC3037sudo inside a script
The script invokes sudo. It will either prompt interactively in the middle of a run or silently require passwordless sudo.
Suggested safeguard: Check privileges up front (if [[ $EUID -ne 0 ]]) and fail with a clear message, rather than escalating mid-script.
Predictable /tmp path
Fixed path under /tmp. Predictable temp paths are a symlink-attack vector on shared machines and collide between concurrent runs.
Suggested safeguard: Create the file with mktemp (or mktemp -d) and remove it in a trap.
SC2129Parsing ls output
Parsing the output of ls. Filenames containing spaces or newlines break the pipeline.
Suggested safeguard: Use a glob or find -print0 with a while IFS= read -r -d '' loop.
SC2012read without -r
read without -r mangles backslashes in the input.
Suggested safeguard: Use read -r (and IFS= read -r line when preserving leading whitespace).
SC2162Missing shebang
No shebang on the first line, so the interpreter depends on how the file is invoked.
Suggested safeguard: Add #!/usr/bin/env bash, #!/bin/sh or #!/usr/bin/env zsh as the first line.
SC2148Missing strict mode
Without set -e, -u (and pipefail in Bash/Zsh) the script continues after failed commands.
Suggested safeguard: Add set -euo pipefail (set -eu in POSIX sh) near the top.
Unbalanced if/fi
The number of if and fi keywords differs, which is a syntax error.
Suggested safeguard: Close every if with a matching fi.
Unbalanced do/done
The number of do and done keywords differs, which is a syntax error.
Suggested safeguard: Close every for/while/until body with done.