ShellCheck flags most of these as SC2086
An unquoted > $log is SC2086, and an unquoted > $(cmd) is SC2046. One of the four causes below gets past ShellCheck entirely; that one is worth reading. Paste any other finding into the ShellCheck Error Decoder.
The backup script logs to a file named after the job. Someone adds a job with a space in its name, and the next night's log shows one line: $log: ambiguous redirect. The error does not say the backup failed. It did, because bash never ran the command on that line at all, and the script carried on to the next one.
Does the Command Still Run After an Ambiguous Redirect?
No. Reproduced here on 2026-11-03 (Kali 2026.3, GNU bash 5.3.15, GNU tar 1.35), in a scratch directory with a data/ folder to archive:
Bash opens a command's redirections before it starts the command. When > $log cannot be opened, the command is dropped, so tar never ran and backup.tgz does not exist. Exit status 1 is the only trace, and with no set -e the script went straight on to the next line. A redirection on a loop behaves the same way: for f in data/*; do …; done > $log runs zero iterations and prints the same error.
Which Values Cause "ambiguous redirect"?
Bash word-splits and glob-expands an unquoted redirection target, then needs exactly one word. Four ways to get zero or two, run as one script with app.log and err.log present:
| Target (unquoted) | Expands to | bash 5.3 result |
|---|---|---|
$file = my report.txt | 2 words | $file: ambiguous redirect, exit 1 |
$outfile unset or empty | 0 words | $outfile: ambiguous redirect, exit 1 |
*.log matching 2 files | 2 words | *.log: ambiguous redirect, exit 1 |
$(cmd) printing nothing | 0 words | $(cmd): ambiguous redirect, exit 1 |
*.nomatch matching nothing | 1 word, the literal pattern | no error: writes a file named *.nomatch |
The error repeats the target as you typed it, not its value, so $file: ambiguous redirect tells you which variable to inspect but not what was in it. The last row is the quiet one: a glob with no matches is left as-is, and afterwards the directory held a new 6-byte file literally named *.nomatch: hello plus a newline. A glob that matches exactly one file writes to that file, which is how > *.log works for months and then breaks the day a second log appears.
The same messages came out of GNU bash 5.2.21 in an ubuntu:24.04 container and 5.3.9 in ubuntu:26.04.
Why Does the Same Line Work Under sh?
On Kali, Debian and Ubuntu, /bin/sh is dash (0.5.12 here). The same b.sh, run in a fresh directory with dash b.sh:
dash never prints ambiguous redirect. It does not split or glob a redirection target, so > $file wrote my report.txt and > *.log wrote a third file literally named *.log next to the two real logs. Only the empty cases fail, as cannot create : Directory nonexistent with exit 2. bash --posix b.sh matched dash on the space and glob lines (no error, same files created) and still printed ambiguous redirect for the two empty ones.
So a script that "works with sh script.sh" and fails with bash script.sh is usually this: the unquoted target was always one word too many for bash, and dash was quietly writing to a filename with a space or a * in it.
How Do I Fix "ambiguous redirect"?
Quote the target. A quoted expansion is never split or globbed, so it is always exactly one word:
Quoting does not fix an empty variable. It changes the error to one that hides the cause:
Nothing before the colon: bash tried to open a file whose name is the empty string. For a path that must be set, make the script name the variable and stop:
Both exited before next, which is what you want from a script whose output file is missing. set -e alone is not enough here: in a run with set -e and LOGFILE unset, bash still printed $LOGFILE: ambiguous redirect and exited 1. It stopped, but the message named the redirect, not the variable.
The order to apply them:
set -euo pipefail at the top of the script covers the unset case for every variable you forget to check.
Does ShellCheck Catch "ambiguous redirect"?
Three of the four. ShellCheck 0.11.0 on the same lines:
Line 4 of that file is echo hello > *.log, and ShellCheck said nothing about it. A literal glob as a redirection target is not flagged, so search for it yourself. With GNU grep 3.12, on a test file:
It lists unquoted globs after > or >> and skipped the quoted > "$f" on line 3. SC2086 is info severity, the level most people filter out; for redirection targets it is the warning for this exact failure.
Prerequisites
bash (any version; the messages above are identical on 5.2.21 and 5.3) and, for the check, ShellCheck: apt install shellcheck, dnf install ShellCheck or brew install shellcheck. No root.
Frequently Asked Questions
What does ambiguous redirect mean in bash?
It means the word after a redirection operator such as >, >> or 2> expanded to zero words or to more than one. Bash applies word splitting and filename expansion to an unquoted redirection target, so a value with a space, an empty or unset variable, a glob matching several files, or a command substitution that prints nothing leaves bash without exactly one filename. It prints NAME: ambiguous redirect, where NAME is the word as you typed it, and the command fails with exit status 1.
Does the command still run after an ambiguous redirect?
No. Bash sets up redirections before it runs the command, and when a redirection fails the command is skipped entirely. tar czf backup.tgz data > $log with log set to a name containing a space never runs tar, so no backup.tgz is created. The same applies to a whole loop: for … done > $log runs zero iterations. Without set -e the script then continues on the next line as if nothing happened, apart from the exit status 1.
Why does the same script work with sh but fail with bash?
On Debian, Ubuntu and Kali, sh is dash, and dash does not word-split or glob-expand a redirection target. echo hello > $file with file="my report.txt" writes a file named my report.txt, and > *.log writes a file literally named *.log. bash --posix behaves the same way for those two cases. An empty or unset variable fails in both shells, but dash reports it as cannot create : Directory nonexistent with exit status 2 instead of ambiguous redirect.
Why do I get No such file or directory after quoting the variable?
Quoting keeps an empty value as one empty word, so bash tries to open a file whose name is the empty string and the kernel refuses with ENOENT. The message is : No such file or directory, with nothing before the colon because the filename is empty. Quoting fixed the splitting; the real problem is that the variable was never set. Use "${log:?log is not set}" to stop with the variable's name, or give it a default with "${log:-/tmp/run.log}".
How do I redirect output to a file with spaces in the name?
Double-quote the target: echo hello > "$file" or tar czf backup.tgz data > "backup log.txt". Quotes stop word splitting and globbing, so the whole value reaches open() as one filename. Use double quotes, not single quotes, when the name comes from a variable, because single quotes prevent the variable from expanding.
The safe bash script template starts every script with set -euo pipefail and :? checks on required paths for this reason, and The Production Bash Toolkit ships that template ShellCheck-clean.
Part of the bash snippets collection
Related Scripts
- Fix "unary operator expected" in Bash — the same empty-variable bug inside
[ ]instead of after> - Bash Environment Variables —
${VAR:-default}and${VAR:?message}in depth - Bash Error Handling —
set -euo pipefail, and whatset -ucatches thatset -edoes not - scp Command Examples — scp's own "ambiguous target" error, the remote-side version of a path with spaces
- Bash Error Messages, Decoded — nine other errors, each with the one check that names the cause