I want to know if there is a built-in BASH command that prints some text on stderr, just like the echo
command that prints text on stdout. I don't want to use temporary io-redirection. I use a built-in command to generate an error on stderr such as ls --asdf
(ls: unrecognized option '--asdf') but I want something neater.
Edit ----
Actually I am trying to demonstrate stderr/stdout redirection, and my example looks like:
sh test.sh >test-out.txt 2>test-err.txt
For clarity, I want to keep the test.sh file as simple and clean as possible, this means avoiding >
operator inside the file.
The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.
Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.
The error message that is delivered via stderr is still sent to the terminal window. We can check the contents of the file to see whether the stdout output went to the file. The output from stdin was redirected to the file as expected. The > redirection symbol works with stdout by default.
No one has mentioned this but you can also do this:
echo An error message > /dev/stderr
It's possibly more readable than >&2
but I guess that depends who you are.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With