Getting an "ambiguous redirect" error
BashBash Problem Overview
The following line in my Bash script
echo $AAAA" "$DDDD" "$MOL_TAG >> ${OUPUT_RESULTS}
gives me this error:
line 46: ${OUPUT_RESULTS}: ambiguous redirect
Why?
Bash Solutions
Solution 1 - Bash
Bash can be pretty obtuse sometimes.
The following commands all return different error messages for basically the same error:
$ echo hello >
bash: syntax error near unexpected token `newline`
$ echo hello > ${NONEXISTENT}
bash: ${NONEXISTENT}: ambiguous redirect
$ echo hello > "${NONEXISTENT}"
bash: : No such file or directory
Adding quotes around the variable seems to be a good way to deal with the "ambiguous redirect" message: You tend to get a better message when you've made a typing mistake -- and when the error is due to spaces in the filename, using quotes is the fix.
Solution 2 - Bash
Do you have a variable named OUPUT_RESULTS
or is it the more likely OUTPUT_RESULTS
?
michael@isolde:~/junk$ ABC=junk.txt
michael@isolde:~/junk$ echo "Booger" > $ABC
michael@isolde:~/junk$ echo "Booger" >> $ABB
bash: $ABB: ambiguous redirect
michael@isolde:~/junk$
Solution 3 - Bash
put quotes around your variable. If it happens to have spaces, it will give you "ambiguous redirect" as well. also check your spelling
echo $AAAA" "$DDDD" "$MOL_TAG >> "${OUPUT_RESULTS}"
eg of ambiguous redirect
$ var="file with spaces"
$ echo $AAAA" "$DDDD" "$MOL_TAG >> ${var}
bash: ${var}: ambiguous redirect
$ echo $AAAA" "$DDDD" "$MOL_TAG >> "${var}"
$ cat file\ with\ spaces
aaaa dddd mol_tag
Solution 4 - Bash
I've recently found that blanks in the name of the redirect file will cause the "ambiguous redirect" message.
For example if you redirect to application$(date +%Y%m%d%k%M%S).log
and you specify the wrong formatting characters, the redirect will fail before 10 AM for example. If however, you used application$(date +%Y%m%d%H%M%S).log
it would succeed. This is because the %k
format yields ' 9'
for 9AM where %H
yields '09'
for 9AM.
echo $(date +%Y%m%d%k%M%S)
gives 20140626 95138
echo $(date +%Y%m%d%H%M%S)
gives 20140626095138
The erroneous date might give something like:
echo "a" > myapp20140626 95138.log
where the following is what would be desired:
echo "a" > myapp20140626095138.log
Solution 5 - Bash
Does the path specified in ${OUPUT_RESULTS} contain any whitespace characters? If so, you may want to consider using ... >> "${OUPUT_RESULTS}"
(using quotes).
(You may also want to consider renaming your variable to ${OUTPUT_RESULTS}
)
Solution 6 - Bash
One other thing that can cause "ambiguous redirect" is \t
\n
\r
in the variable name you are writing too
Maybe not \n\r
? But err on the side of caution
Try this
echo "a" > ${output_name//[$'\t\n\r']}
I got hit with this one while parsing HTML, Tabs \t
at the beginning of the line.
Solution 7 - Bash
I just had this error in a bash script. The issue was an accidental \ at the end of the previous line that was giving an error.
Solution 8 - Bash
If your script's redirect contains a variable, and the script body defines that variable in a section enclosed by parenthesis, you will get the "ambiguous redirect" error. Here's a reproducible example:
vim a.sh
to create the script- edit script to contain
(logit="/home/ubuntu/test.log" && echo "a") >> ${logit}
chmod +x a.sh
to make it executablea.sh
If you do this, you will get "/home/ubuntu/a.sh: line 1: $logit: ambiguous redirect". This is because
> "Placing a list of commands between parentheses causes a subshell to > be created, and each of the commands in list to be executed in that > subshell, without removing non-exported variables. Since the list is > executed in a subshell, variable assignments do not remain in effect > after the subshell completes."
From Using parenthesis to group and expand expressions
To correct this, you can modify the script in step 2 to define the variable outside the parenthesis: logit="/home/ubuntu/test.log" && (echo "a") >> $logit
Solution 9 - Bash
This might be the case too.
you have not specified the file in a variable and redirecting output to it, then bash will throw this error.
files=`ls`
out_file = /path/to/output_file.t
for i in `echo "$files"`;
do
content=`cat $i`
echo "${content} ${i}" >> ${out_file}
done
out_file variable is not set up correctly so keep an eye on this too. BTW this code is printing all the content and its filename on the console.
Solution 10 - Bash
I got this error when trying to use brace expansion to write output to multiple files.
for example: echo "text" > {f1,f2}.txt
results in -bash: {f1,f2}.txt: ambiguous redirect
In this case, use tee
to output to multiple files:
echo "text" | tee {f1,f2,...,fn}.txt 1>/dev/null
the 1>/dev/null
will prevent the text from being written to stdout
If you want to append to the file(s) use tee -a
Solution 11 - Bash
if you are using a variable name in the shell command, you must concatenate it with +
sign.
for example :
if you have two files, and you are not going to hard code the file name, instead you want to use the variable name
"input.txt" = x
"output.txt" = y
>then ('shell command within quotes' + x > + y)
it will work this way especially if you are using this inside a python program with os.system command probably
Solution 12 - Bash
In my case, this was a helpful warning, because the target variable (not the file) was misspelled and did not exist.
echo "ja" >> $doesNotExist
resulting in
./howdy.sh: line 4: $doesNotExist: ambiguous redirect