Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash command that prints a message on stderr

Tags:

linux

bash

shell

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.

like image 773
Salman A Avatar asked Apr 15 '10 06:04

Salman A


People also ask

How do I find the output of stderr?

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.

How do I print stderr from stdout?

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.

Does stderr print to terminal?

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.


1 Answers

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.

like image 143
nic ferrier Avatar answered Sep 23 '22 04:09

nic ferrier