-2

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
Charles Duffy
  • 235,655
  • 34
  • 305
  • 356
pato.llaguno
  • 711
  • 4
  • 20

1 Answers1

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

Community
  • 1
  • 1
Kyle
  • 3,720
  • 1
  • 19
  • 22