Unix/Linux Go Back    

UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

/dev/null 2>&1 question

UNIX for Dummies Questions & Answers

Thread Tools Search this Thread Display Modes
Old Unix and Linux 04-29-2003   -   Original Discussion by BAM
BAM's Unix or Linux Image
BAM BAM is offline
Registered User
Join Date: Jan 2002
Last Activity: 4 September 2004, 9:28 AM EDT
Location: Lebanon
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
/dev/null 2>&1 question


suppose you have the following line at your crontab :

5 * * * * /usr/mine/script > /dev/null 2>&1

now i understood that the " > /dev/null 2>&1 outputs both Standard outpout and Standard Error messages to the /dev/null device or file...

the first part , " > /dev/null " transfers the stdout messages to the /dev/null, but i am missing the part where the stderr is also dumped at dev/null.....

2(is stderr) >(send) &1 (stdout)...

i am not understanding how you send stdout first to dev/null and then send stderr to stdout which in turn will be sent to dev/null too ???

Sponsored Links
Old Unix and Linux 04-29-2003   -   Original Discussion by BAM
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Advisor  
Unix Daemon (Administrator Emeritus)
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 63
Thanked 474 Times in 272 Posts
There is a system file table. It has one entry for every open file. Your process has descriptors that point to file table entries. Let's change the example to a text file.

So the shell syntax starts out " > results.txt". To run that the shell will need to do something like:
fd=open("results.txt", O_WRONLY, 0);
We actually had to open a file. This filled in a file table entry. And now fd 0 points to that file table entry.

Now the syntax continues "2>&1" and to run that, the shell does something like:
Here we don't open a file. It's just that whatever file table entry fd 1 was pointing to has been copied into fd 2.

There is more to this than just saving an expensive open call. If we write to fd 1, what ever we write goes into the file. If we then write some more, the new data goes at the end of the file. We can write to fd 1 and we can write to fd 2 and they don't step on each other. That's because a write to either fd updates the file pointer in the file table entry.

In contrast, " > results.txt 2 > results.txt" gives us two file table entries and two file pointers.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Redirecting standard out to /dev/null goes to file "/dev/null" instead jbmorrisonjr Shell Programming and Scripting 5 06-06-2012 07:27 PM
Question on NULL and zero value of variable spynappels Shell Programming and Scripting 7 09-28-2011 09:32 AM
Newbie question: if[command not null] zangarules Shell Programming and Scripting 13 04-13-2011 04:28 PM
Insert string 'NULL' where there is a null value zilch Shell Programming and Scripting 8 05-27-2010 09:17 AM
Question about NULL Character & fgets() f.ben.isaac Programming 2 11-04-2008 11:31 AM

All times are GMT -4. The time now is 01:08 PM.