How to print a line to STDERR and STDOUT in Bash?

Posted on In QA

In Bash, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely?

And similarly, how to print the line to STDERR?

In Bash, you can simply use the echo command:

echo "your message here"

or

echo your message here

Examples:

$ echo the message here
the message here
$ echo "the message here"
the message here
$ echo `date`
Fri Apr 7 13:40:20 HKT 2017
$ 

To STDERR:

In Bash:

1>&2 echo "msg to STDERR"

The 1>&2 redirects STDOUT to STDERR.

Leave a Reply

Your email address will not be published. Required fields are marked *