2

I want to list all modules of nginx server by line, so I type the following:

nginx -V | tr -- - '\n'

But it doesn't put modules in lines. If I do it like this:

2>&1 nginx -V | tr -- - '\n'

it works. But why?

4
  • 2
    because nginx -V writes its output to the standard error stream (file descriptor 2) rather than the standard output stream (file descriptor 1)? Nov 8, 2014 at 11:11
  • aha, thanks, and basically this 2>&1 says redirect output from error stream (2) to standart output (1) ? Nov 8, 2014 at 11:26
  • yes that's correct - I have expanded my comment as an answer Nov 8, 2014 at 12:32
  • I know this is old, and applies to Linux / Ubuntu, but this ALSO applies to Windows. They are still doing this, even at version 1.22.1. Life saver right here - been searching for this for hours!
    – k1dfr0std
    Jan 29 at 23:19

1 Answer 1

5

What you're observing indicates that nginx -V writes its output to the standard error stream (aka stderr - on file descriptor 2) rather than the standard output stream (aka stdout - file descriptor 1). Only stdout gets passed through the pipe | so your tr command doesn't receive it.

The 2>&1 syntax tells the shell to redirect stderr to stdout so that it then gets piped to tr. FYI the placement you have used is syntactically legal but unconventional - most often that kind of redirection would be written as

nginx -V 2>&1 | tr -- - '\n'

BTW the pipe | is really redirecting the output of the first command rather than the input of the second, which is coming from the standard input stream (stdin - file descriptor 0) as usual.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .