Ambiguous output redirect

25,815

If you're running (t)csh, you get Ambiguous output redirect. if you try to set up two conflicting redirections:

> echo foo > a > b
Ambiguous output redirect.

In Bash, you could get a similar error if use an array with multiple elements in place of the filename:

$ set aa bb
$ echo foo > "$@"
bash: "$@": ambiguous redirect

As mentioned in answers to stderr redirection not working in csh, the >& operator works in (t)csh to redirect both stdout and stderr. 2>&1 is the standard way to redirect stderr to the same place as stdout, but (t)csh doesn't support that. Instead, it takes the combination > foo 2>&1 as a redirection to foo, a regular argument 2, and a redirection to 1, and the redirections conflict, so you get the error.

>& also works in Bash and zsh, but isn't a standard feature.

Share:
25,815

Related videos on Youtube

jbeaulau
Author by

jbeaulau

Updated on September 18, 2022

Comments

  • jbeaulau
    jbeaulau over 1 year

    I'm trying to redirect stderr to stdout and then out to a file in an init script, but when I introduce stderr to stdout I get the “Ambiguous output redirect” error. Stdout alone does not result in the error, and writes to the log file where I stated. I've tried the following

    -jar /jbeaulau_test/microservices/config-server-0.0.2-RELEASE.jar &>/jbeaulau_test/microservices/log/all.log &
    
    -jar /jbeaulau_test/microservices/config-server-0.0.2-RELEASE.jar >/jbeaulau_test/microservices/log/all.log 2>&1 &
    

    Any advice would be appreciated.

  • fugitive
    fugitive over 6 years
    The ">/tmp/x.txt" part will redirect stdout (file handle #1). It must not contain any spaces. . It can contain the space. command >out.txt == command > out.txt
  • Kevin Keane
    Kevin Keane over 6 years
    You are right; I made a mistake there. Fixing it.
  • Wildcard
    Wildcard over 6 years
    "The first command you wrote is simply a syntax error." No, it's not; it's the preferred Bash syntax for redirecting both stdout and stderr. See LESS='+/Redirecting Standard Output and Standard Error' man bash
  • Kevin Keane
    Kevin Keane over 6 years
    @Wildcard - thanks. I never stop learning!