How to print on stderr without using /dev/stderr [

2019-09-08 13:01发布

How can a linux command script print warnings or error messages without using >/dev/stderr each time?

This should be simple but I don't get it.

Can you post a simple example?

2条回答
劳资没心,怎么记你
2楼-- · 2019-09-08 13:32

To print to standard error, use a command that writes to standard output (like echo or printf) and redirect the output to file descriptor 2.

echo "This goes to standard output"
echo "This goes to standard error" >&2

This is the most common use of the file descriptor duplication operator, which makes the descriptor indicated by the preceding number (or 1 if omitted) a copy of the descriptor indicated by the following number.

查看更多
冷血范
3楼-- · 2019-09-08 13:47

Every unix process always has three open file descriptors when the process starts.

0 is stdin

1 is stdout

2 is stderr

http://man7.org/linux/man-pages/man3/stdout.3.html

查看更多
登录 后发表回答