LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-19-2011, 10:12 PM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 31
What is 2>&- mean


In my linux system , there is a script that have a 2>&- , I know this is to handle error message , can advise what is this mean ?

Thanks.
 
Old 09-19-2011, 10:27 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hello,

It is just piping STDERR to the background, running it in the background using the & sign. Have a look here - http://en.wikipedia.org/wiki/Standard_streams
Always remember the following file descriptors:
Code:
0 - STDIN
1 - STDOUT
2 - STDERR
Cheers,

Josh
 
1 members found this post helpful.
Old 09-19-2011, 10:54 PM   #3
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 31
Thx reply,

what is - sign mean ?
 
Old 09-19-2011, 11:11 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Could you show me what the script does, and what output you get? I honestly never used a - with file descriptors.
 
Old 09-20-2011, 03:57 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by corp769 View Post
It is just piping STDERR to the background, running it in the background using the & sign.
Incorrect. The &- closes the designated file descriptor. So what it's doing is turning stderr completely off, instead of redirecting it into /dev/null or similar, as scripters usually do.

http://wiki.bash-hackers.org/syntax/redirection
 
1 members found this post helpful.
Old 09-20-2011, 06:47 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by David the H. View Post
Incorrect. The &- closes the designated file descriptor. So what it's doing is turning stderr completely off, instead of redirecting it into /dev/null or similar, as scripters usually do.

http://wiki.bash-hackers.org/syntax/redirection
Ahh, I didn't know that about the dash. Thanks for that man, I even tried looking for that online, but didn't know what it was called, along with being part of the file descriptors.
 
Old 09-20-2011, 07:15 PM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Nevertheless there is a difference in closing the file descriptor or redirecting it to /dev/null. To /dev/null you can write to, while writing to a closed file descriptor yields write error: Bad file descriptor.

I would even say the alternative way explained in http://wiki.bash-hackers.org/syntax/redirection is not clean, as the error about not being able to write to stdout can’t also be output. As a consequence the return code of an application made quiet this way might be wrong (i.e. indicating an error).

N.B. It’s in the bash man page in the section about copying file descriptors.

Last edited by Reuti; 09-21-2011 at 04:36 AM. Reason: Clarified the return code indicating an error / Typo
 
Old 09-20-2011, 07:29 PM   #8
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by Reuti View Post
Nevertheless there is a difference in closing the file descriptor or redirecting it to /dev/null. To /dev/null you can write to, while writing to a closed file descriptor yields write error: Bad file descriptor.

I would even say the alternative way explained in http://wiki.bash-hackers.org/syntax/redirection is not clean, as the error about not being able to write to stdout can’t also be output. As a consequence the return code an application made quiet this way might be wrong (i.e. indicating an error).

N.B. It’s in the bash man page in the section about copying file descriptors.
Ahh, that's why I couldn't find it, I was looking in the wrong places. But after doing the research and fully understanding what it does and how it works, yeah that is true on what you said. You could always run the output before hand to redirect, in that case, to get the output redirected properly.
 
Old 09-21-2011, 03:08 AM   #9
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
It was late this morning. Now with a fresh “proof of error”:
Code:
$ ls >&-
ls: write error: Bad file descriptor
$ echo $?
2
$ ls >/dev/null
$ echo $?
0
Same behavior on AIX: so I think it’s not wise to disregard the output this way. Interesting though, that on Mac OS X it’s really just working as advertised. But I would even this judge as an error: it shouldn’t be possible to write to a closed file descriptor without notifying the user about the error (even a test whether /dev/stdout is writable succeeds, but maybe it’s the way BSD works).

The syntax is fine in case you close a file descriptor you no longer need:
Code:
$ exec 5>foobar
$ echo Hello >&5
$ echo World >&5
$ exec 5>&-

Last edited by Reuti; 09-21-2011 at 04:36 AM. Reason: Typo
 
  


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
zsnes && logitech dual action gamepad && dpad woes John5788 Linux - Games 5 10-29-2008 09:56 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
(FreeBSD && Fedora Core 4 && Slackware 10.0) Filesystem Support taylor_venable *BSD 1 07-14-2005 02:24 PM
Japanese canna won't work : Warning: かな漢字変&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 12:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:47 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
Open Source Consulting | Domain Registration