[bash] Getting an "ambiguous redirect" error

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?

This question is related to bash

The answer is


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.


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:

  1. vim a.sh to create the script
  2. edit script to contain (logit="/home/ubuntu/test.log" && echo "a") >> ${logit}
  3. chmod +x a.sh to make it executable
  4. a.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


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


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

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})


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.


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

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.


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$ 

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