Common errors

What command not found, unbound variable, unary operator expected, syntax error near unexpected token and Read-only file system mean and how to fix them.

Runs in Zerethon IDE · Bash 5.2 Official Bash documentation ↗

Bash names the file and line: main.sh: line 4: …. A failed command does not stop the script unless set -e is on, so one error can cause others further down — fix the first one first.

  • count: command not found after count = 3 — remove the spaces around =.
  • unbound variable — with set -u, a variable was used before it was set; give it a value or a default ${x:-}.
  • [: too many arguments / unary operator expected — an unquoted variable is empty or has spaces; quote it ("$x") or use [[ ]].
  • integer expression expected — -eq / -lt got text that is not a whole number.
  • syntax error near unexpected token 'fi' — a missing then, do or ;, or a missing space inside [[ ]].
  • No such file or directory — the path is relative to the project root; check spelling and capitals.
  • Read-only file system — the project files cannot be changed during a run; write to /tmp.
  • value too great for base — a number with a leading zero, such as 08, in arithmetic.
main.sh
total=0
for v in 10 x 7; do
  if [[ $v =~ ^-?[0-9]+$ ]]; then
    (( total += v ))
  else
    echo "skipped '$v'"
  fi
done
echo "total: $total"

Output

skipped 'x'
total: 17
Check input before using it as a number.

Tip: Add set -x to print every command before it runs — the quickest way to see what a script is really doing.