Check syntax without running
Check a script for syntax errors without running it using bash -n. It finds an unclosed if, but not a misspelled command or a missing file.
A deploy script was edited by hand, and you want to know that it parses before it touches anything.
cat > broken.sh <<'EOF'
#!/bin/bash
if [ -f /etc/os-release ]; then
echo "found"
EOF
cat > typo.sh <<'EOF'
#!/bin/bash
ecoh "this command does not exist"
EOF
bash -n broken.sh; echo "broken.sh: $?"
bash -n typo.sh; echo "typo.sh: $?"
bash typo.sh; echo "run typo.sh: $?"broken.sh: line 4: syntax error: unexpected end of file
broken.sh: 2
typo.sh: 0
typo.sh: line 2: ecoh: command not found
run typo.sh: 127With -n Bash reads and parses the commands but executes none of them, so a missing fi is reported with a line number and the exit status is 2, as the set builtin documentation explains for noexec. The second file parses cleanly, because a misspelled command name is a valid command to the parser, and only the real run finds it.
The check cannot see runtime failures, unset variables or bad paths, so treat it as the first and cheapest gate, and follow it with ShellCheck. It parses only the file itself, and a sourced file is not read. Do not use set -n in a long running interactive shell, since it also stops later commands.