What does 1>&2 mean in shell? [duplicate]

爱⌒轻易说出口 提交于 2020-01-03 06:42:12

问题


Pretty noob question, what does the 1>&2 do in this script?

if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

回答1:


That redirects the line "This script must be run as root" from standard out (STDOUT) to standard error output (STDERR).

It's a easy way to print an error message to STDERR - this matters if you run the bash script from another script (like crontab), matters much less if you run it from the command line driectly since your terminal will show both STDOUT and STDERR.

See also echo that outputs to stderr



来源:https://stackoverflow.com/questions/26225771/what-does-12-mean-in-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!