Zsh script review: option state matters

Review Zsh option state, Bash-only builtins, scalar splitting and destructive commands without applying misleading Bash assumptions to Zsh scripts.

Zsh resembles Bash but does not share every expansion or builtin. Copying a Bash warning into a Zsh report can be wrong even when the source looks identical. Begin with the interpreter, option changes and whether the file is a standalone script or is sourced into an existing session. A review must distinguish visible option settings from a caller state it cannot observe.

Understand scalar splitting before quoting advice

By default, an unquoted Zsh scalar containing spaces does not undergo the same automatic word splitting as Bash. SH_WORD_SPLIT or an explicit splitting expansion changes that behavior. Quoting is still useful for clear argument boundaries, especially at interfaces with other shells, but a generic Bash SC2086 explanation is not proof of a Zsh defect. Arrays are a natural representation for several arguments; check whether the code intends a scalar, an array or an explicitly split string.

Check builtins and emulation modes

shopt, mapfile and readarray are Bash builtins rather than native Zsh commands. Their presence deserves a compatibility review unless the file supplies a replacement function or external executable. Zsh uses setopt for options and offers its own parameter expansion facilities. emulate can change behavior toward another shell, so read its scope before interpreting later commands. Do not replace a builtin with a superficially similar command without checking what values and statuses the original caller expects.

Treat glob behavior as part of the contract

An unmatched filename pattern can cause an error under Zsh option settings where another shell would pass the pattern literally. NULL_GLOB and related options change that contract. A script processing optional file sets should make the intended no-match outcome explicit instead of relying on your interactive configuration. Paths can also carry glob qualifiers or expansion flags. The analysis should state the syntax it observes and avoid asserting the resulting file list without access to the filesystem.

Keep safety checks independent of shell preference

A recursive removal with a variable path, an unchecked cd, dynamic eval and a downloaded script passed to a shell remain important review topics in Zsh. A different splitting default does not validate a destructive destination. Check for empty values, allowed directories, explicit failure guards and the source of any executable string. ERR_EXIT, NO_UNSET and pipeline options can expose failures, but the complete control-flow context still matters when a command is tested as a condition.

Review example

The sample contains a Bash builtin in a Zsh script and independent path-safety concerns. Review those facts without assuming Bash scalar-splitting behavior.

#!/usr/bin/env zsh
shopt -s nullglob
cd /var/app
rm -rf $BUILD_DIR/*
Open the checker

What cannot be verified

The checker does not load your .zshrc, inspect a caller session or evaluate expansion results. Sourced files can depend on state not present in the submitted text. Report that state as unknown and confirm target Zsh versions before choosing a compatibility patch. Native Zsh options and arrays should be reviewed on their own terms, not modernized into Bash without an explicit requirement.

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".

SC2115

Piping 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.

SC2294

World-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.

SC2164

Legacy backtick substitution

Legacy backtick command substitution. It nests badly and handles backslashes differently from the modern form.

Suggested safeguard: Use $(command) instead of `command`.

SC2006

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.

SC3037

sudo 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.

SC2129

Parsing 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.

SC2012

read without -r

read without -r mangles backslashes in the input.

Suggested safeguard: Use read -r (and IFS= read -r line when preserving leading whitespace).

SC2162

Bash-only builtins in a Zsh script

shopt, mapfile and readarray are Bash builtins and do not exist in Zsh.

Suggested safeguard: Use setopt for options and ${(f)"$(cmd)"} to split command output into lines.

Missing 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.

SC2148

Missing 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.

Official references