Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

bash - Redirection of echo's standard output to standard error, still produces output in terminal?

I'm a bit confused as to why when I'm redirecting the standard output of an echo command to the standard error, why I still get the argument printed in the terminal?

Here's is the line I ran

echo "potato" >&2

Could someone explain this to me? How does this command output anything, if the output was redirected somewhere else?

Thank you :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think what you want is:

bash-3.2$ echo "potato" &>2
bash-3.2$ 

From the man page for bash:

Redirecting Standard Output and Standard Error
       Bash  allows  both  the  standard  output (file descriptor 1) and 
       the standard error output (file descriptor 2) to be redirected to the file 
       whose name is the expansion of word with this construct.

       There are two formats for redirecting standard output and standard error:

              &>word
       and
              >&word

       Of the two forms, the first is preferred.  This is semantically equivalent to

              >word 2>&1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...