5

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.

jayhendren
  • 8,384
  • 2
  • 33
  • 58

2 Answers2

6

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.

ilkkachu
  • 138,973
-1

The second entry should work fine. The "ambiguous redirect" error sometimes happens if you either have spaces where they shouldn't be, or conversely when an important space is missing.

I would simplify your command to demonstrate:

echo "Test" >/tmp/x.txt 2>&1 &

The ">/tmp/x.txt" part will redirect stdout (file handle #1). A space between the > and the file name is permitted (although in this context would be confusing), but otherwise there should not be any spaces in here.

The 2>&1 will redirect stderr (file handle 2) to whatever file handle 1 goes to (which is stdout). There must not be any spaces in here, either.

The & will background your task. This must be offset with a space from the preceding character.

Reversing the two redirections does not work (although echo is a poor choice here since it does not produce stderr output):

echo "This will not work" 2>&1 >/tmp/x.txt &

This means:

2>&1

Redirect file handle 2 to where file handle 1 goes (which at this point is still the console)

>/tmp/x.txt

Redirect file handle 1 to a file - but since file handle 2 (stderr) is already redirected at this point, it will keep its destination and still go to the console.

The first command you wrote is simply a syntax error.

echo &>/tmp/x.txt

Update: @Wildcard pointed out in the comments that this is actually valid syntax.

  • 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 – fugitive Jan 23 '18 at 00:57
  • You are right; I made a mistake there. Fixing it. – Kevin Keane Jan 23 '18 at 01:09
  • "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 – Wildcard Jan 23 '18 at 03:32
  • @Wildcard - thanks. I never stop learning! – Kevin Keane Jan 23 '18 at 17:15