what does ">&" mean when followed by a file name?
I encountered a strange command when using Bourne Shell:
echo 123 >& result
Typically I would expect something like echo 123 > result 2>&1
, I never expect >&
would be followed by a开发者_StackOverflow中文版 file name.
But to my surprise, when I execute this command in shell, the result is correct, it did create a file named "result", and this file contained text "123".
I am rather confused by this grammar. Can anyone explain this to me ?
This is a Bash extension meaning redirect both stdout and stderr to the result file.
A portable way to achieve the same with POSIX compliant shells would be:
echo 123 >bar 2>&1
As "echo 123" is unlikely to output anything on stderr, that syntax is useless here.
Here is a example showing it working:
(echo stderr 1>&2;echo stdout) >& foo
(echo stderr 1>&2;echo stdout) >bar 2>&1
File descriptor of STDOUT is 1 which is used by default if you do not mention any file descriptor explicitly. So if I remember it correctly echo 123 >& result
is same as echo 123 1>&1 result
case solved :
echo 123 >& result
is equivalent to echo 123 > result 2>&1
精彩评论