LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2011, 06:43 PM   #1
skiabox
Member
 
Registered: Dec 2006
Posts: 44

Rep: Reputation: 0
Why should I use >&2 after echoing an error


I am reading a book about bash shell and I see that most of the scripts redirect the error message to >&2.
What is the purpose of this act?
Thank you.
 
Old 10-23-2011, 07:00 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by skiabox View Post
I am reading a book about bash shell and I see that most of the scripts redirect the error message to >&2.
What is the purpose of this act?
Thank you.
So that the message goes out of stderr instead of stdout.

https://secure.wikimedia.org/wikiped...andard_streams
 
Old 10-24-2011, 10:14 AM   #3
skiabox
Member
 
Registered: Dec 2006
Posts: 44

Original Poster
Rep: Reputation: 0
I've read the whole article but I still cannot understand the usefulness of this command.
Can you give an example?
Thank you.
 
Old 10-24-2011, 10:45 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
If you are running a script and it has the ability to continue through certain errors but you wish to log or view those errors then the standard practice is to capture
whatever is sent to stderr whilst all correct output is sent to stdout. I believe if you execute some of the scripts you are looking at it will become clearer.
 
Old 10-24-2011, 03:57 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Try running these commands, and check the contents of the file after each:

Code:
{ echo "this is important text" && echo "this is an error" ;} | cat > file.txt

{ echo "this is important text" && echo "this is an error" >&2 ;} | cat > file.txt
(Yes, cat is unnecessary here, but it's for demonstration purposes.)


By default stderr prints to the same screen as stdout, but it's treated as a separate stream, and so is ignored by anything that reads from stdin. It doesn't pass through pipes or get redirected by the regular > and >> operators (unless you specifically tell it to).

So what you're doing is telling the shell that the text is just such an error message, rather than part of the main output.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with echoing variables sophisticate Linux - Newbie 6 01-08-2008 12:45 PM
JAVA: echoing current user indienick Programming 17 05-05-2006 01:49 AM
Mp3 are echoing hellfire73 Linux - Software 6 03-19-2006 12:10 AM
Problem of echoing to a file happy78 Programming 7 09-28-2005 12:17 AM
Echoing the first and last colums of a textfile ganninu Linux - General 4 06-26-2004 06:54 AM


All times are GMT -5. The time now is 02:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration