wangshuaijie
wangshuaijie

Reputation: 1931

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 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 ?

Upvotes: 3

Views: 232

Answers (3)

jlliagre
jlliagre

Reputation: 30813

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

Upvotes: 2

wangshuaijie
wangshuaijie

Reputation: 1931

case solved :

echo 123 >& result is equivalent to echo 123 > result 2>&1

Upvotes: 1

Aziz Shaikh
Aziz Shaikh

Reputation: 16524

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

Upvotes: 1

Related Questions