How to print to stderr in the sink?

In a shell script, how do you print the error message to stderr?

For example, this message should go to the stderr stream, not the default stdout stream.

echo "Error: $argv[1] is not a valid option"

      

+3


source to share


1 answer


You can redirect the output to stderr, for example:

echo "Error: $argv[1] is not a valid option" 1>&2

      




As a reference, there are some common IO redirects that work in fish *.

foo 1>&2 # Redirects stdout to stderr, same as bash

bar 2>&1 # Redirects stderr to stdout, same as bash

bar ^&1  # Redirects stderr to stdout, the fish way using a caret ^

      

* The file descriptors for stdin, stdout and stderr are 0, 1, and 2.
* &

implies that you want to redirect to a file stream, not a file.
* Comparison of redirection in different shells (bash, fish, ksh, tcsh, zsh)

+7


source







All Articles