0

While reading about linux I got the following:

Because stderr is not the same as stdout, error messages will still be seen on the terminal windows in the above example.

If you want to redirect stderr to a separate file, you use stderr’s file descriptor number (2), the greater-than sign (>), followed by the name of the file you want to hold everything the running command writes to stderr:

$ do_something 2> error-file

A special shorthand notation can be used to put anything written to file descriptor 2 (stderr) in the same place as file descriptor 1 (stdout): 2>&1

$ do_something > all-output-file 2>&1

bash permits an easier syntax for the above:

$ do_something >& all-output-file

Here do_something is some command. I am really not getting what is happening underneath in all 3 commands stated above. I know & is used for making a process a background process, but I am not getting the logic. I am new to this flavor please Can someone explain this?

1

Your confusion may be because >& is totally a different thing than &.

>& or <& are used in redirection to point at a file descriptor (fd). The above cases redirect to 1 and 2, which are stdout and stderr respectively.

> means "redirect stdout"

2> means "redirect fd 2 (stderr)"

>&2 means "redirect stdout to fd 2 (stderr)". Note how this differs from >2 which would mean "redirect stdout to a file named 2" -- this is why the & is important. It indicates that the next thing will be an fd, not a filename.

Now of course as you noted, if a & is sitting by itself without a redirection symbol, then it means something completely different -- "return immediately, run the preceding command in the background."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.