I am doing my homework and I have a question about one line. I don't understand what this >&2
means:
if test -d $2
then echo "$2: Is directory" >&2 ; exit 21
fi
bashcommand lineredirect
I am doing my homework and I have a question about one line. I don't understand what this >&2
means:
if test -d $2
then echo "$2: Is directory" >&2 ; exit 21
fi
Best Answer
It is simply displaying the message "
/blah/blah/: Is directory
" tostderr
. Also known as Standard Error which is denoted by&2
.Without the
&2
messages are displayed onstdout
. Also known as Standard Output which is denoted by&1
.More details on displaying messages to
&>2
can be found here:In your command posted, both messages for
stdout
andstderr
will appear on your terminal screen. However some applications will separate thestderr
messages and perform special processing.Most people don't bother redirecting
echo
error messages to>&2
but it is technically the correct way of doing things.For more reading on
stdin
,stdout
andstderr
from user or system administrator perspective see:For a programmers perspective of stdin, stdout, stderr which are &0, &1 and &2 respectively see: