Skip to content

Fix "ambiguous redirect" in Bash: Spaces, Empty Variables and Globs

troubleshootingquotingredirectionshellcheckerror-handling
7 min read

Quick Answer

The bash error $file: ambiguous redirect means the word after > did not expand to exactly one filename. Bash word-splits and glob-expands an unquoted redirection target, so a path with a space (my report.txt) becomes two words, an unset or empty variable becomes zero words, and a glob such as > *.log that matches two files becomes two. Bash cannot choose, so it refuses the redirection and skips the whole command. In a run on bash 5.3.15, tar czf backup.tgz data > $log printed the error, returned exit 1 and never created backup.tgz, and without set -e the script carried on to the next line. The fix is to quote the target, > "$log", so a space survives. For a variable that must be set, write > "${log:?log is not set}" or turn on set -u, which name the variable instead. Run the same unquoted line under sh (dash) and there is no error at all: it writes a file literally named my report.txt.

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:

text
$ cat c.sh log="backup log.txt" tar czf backup.tgz data > $log echo "exit=$?" [ -e backup.tgz ] || echo "no backup.tgz" $ bash c.sh c.sh: line 2: $log: ambiguous redirect exit=1 no backup.tgz

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:

text
$ cat b.sh file="my report.txt" echo hello > $file echo "exit=$?" echo hello > $outfile echo "exit=$?" echo hello > *.nomatch echo "exit=$?" touch app.log err.log echo hello > *.log echo "exit=$?" echo hello > $(true) echo "exit=$?" echo "end" $ bash b.sh b.sh: line 2: $file: ambiguous redirect exit=1 b.sh: line 4: $outfile: ambiguous redirect exit=1 exit=0 b.sh: line 9: *.log: ambiguous redirect exit=1 b.sh: line 11: $(true): ambiguous redirect exit=1 end
Target (unquoted)Expands tobash 5.3 result
$file = my report.txt2 words$file: ambiguous redirect, exit 1
$outfile unset or empty0 words$outfile: ambiguous redirect, exit 1
*.log matching 2 files2 words*.log: ambiguous redirect, exit 1
$(cmd) printing nothing0 words$(cmd): ambiguous redirect, exit 1
*.nomatch matching nothing1 word, the literal patternno 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:

text
$ dash b.sh exit=0 b.sh: 4: cannot create : Directory nonexistent exit=2 exit=0 exit=0 b.sh: 11: cannot create : Directory nonexistent exit=2 end $ ls app.log b.sh err.log *.log my report.txt *.nomatch

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:

text
$ cat h.sh file="my report.txt" echo hello > "$file" echo "exit=$?" $ bash h.sh exit=0 $ cat "my report.txt" hello

Quoting does not fix an empty variable. It changes the error to one that hides the cause:

text
$ cat e.sh echo hello > "$LOGFILE" echo "exit=$?" $ bash e.sh e.sh: line 1: : No such file or directory exit=1

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:

text
$ cat f.sh echo hello > "${LOGFILE:?LOGFILE is not set}" echo "next" $ bash f.sh f.sh: line 1: LOGFILE: LOGFILE is not set exit=1 $ cat g.sh set -u echo hello > $LOGFILE echo "next" $ bash g.sh g.sh: line 2: LOGFILE: unbound variable exit=1

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:

bash
# 1. Quote every redirection target. Spaces and * in the value stop mattering. tar czf backup.tgz data > "$log" # 2. Required path: stop with the variable's name if it is unset or empty. tar czf backup.tgz data > "${log:?log is not set}" # 3. Optional path: fall back to a default instead. tar czf backup.tgz data > "${log:-/tmp/backup.log}"

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:

text
$ shellcheck sc.sh In sc.sh line 3: tar czf backup.tgz data > $log ^--^ SC2086 (info): Double quote to prevent globbing and word splitting. Did you mean: tar czf backup.tgz data > "$log" In sc.sh line 5: echo hello > $(ls *.missing) ^-------------^ SC2046 (warning): Quote this to prevent word splitting. ^-- SC2035 (info): Use ./*glob* or -- *glob* so names with dashes won't become options. For more information: https://www.shellcheck.net/wiki/SC2046 -- Quote this to prevent word splitt... https://www.shellcheck.net/wiki/SC2035 -- Use ./*glob* or -- *glob* so name... https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...

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:

text
$ grep -nE '>>?[[:space:]]*[^"[:space:]]*[*?]' g.sh 1:echo hello > *.log 4:echo hi >> *.txt

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

PAID RESOURCE — $9

The Production Bash Toolkit

An operational script system + a 30-function shared library + a 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →
curl -O bashlib-starter.sh

Get the bashlib starter

Ten functions I source into every script on my own boxes — strict-mode setup, an ERR trap that names the failing line, lock and timeout wrappers, and cleanup that runs on every exit path. After that, a short email only when something breaks on a real box and I've worked out why, about twice a month. One-click unsubscribe.

BashSnippets logo

Written by Travis

Creator of BashSnippets.xyz

bashsnippets.xyz/about

Related Snippets

Frequently Asked Questions

faq — snippet

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.

faq — snippet

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.

faq — snippet

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.

faq — snippet

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

faq — snippet

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.