1833873 Members
3809 Online
110063 Solutions
New Discussion

2>&1

 
SOLVED
Go to solution
Shivkumar
Super Advisor

2>&1

Dear Sirs;

Please see the below command:
/usr/lbin/qs >> /var/adm/qs/qs.log 2>&1

In this what is the meaning of 2>&1 ?
I have seen this in most of the cron jobs and some shell scripts.

What would be repercussion of this if we forget to mentioned 2>&1 ?

Thanks,
Shiv
8 REPLIES 8
Rajeev  Shukla
Honored Contributor

Re: 2>&1

Hi Shiv,
This type of syntax is used to collect the standard output and standard error in the file preceeding it.
Like in your case when the script /usr/lbin/qs runs the standard output and standard error of the files will be appended to the file /var/adm/qs/qs.log and if you miss the syntax 2>&1 then only the output of the command will be in the file.
Have a look at the man pages of sh-bourne for more details

Cheers
Rajeev
Patrick Wallek
Honored Contributor
Solution

Re: 2>&1

If you want to get really technical you need to know what those numbers mean.

Technically those are file descriptors.

File descriptor 0 = standard input
File descriptor 1 = standard output
File descriptor 2 = standard error

Now normally when you do things via a terminal session file descriptors 1 and 2 both come back to the screen.

But if you do a command like:

# ll > afile

That will just redirect file descriptor 1 to the file afile. If there are any errors, then you would see them echo'd to the screen.

If you did:

# ll > afile 2>&1

Then you are telling the shell to redirect file descriptor 2 (standard error) to the same location as file descriptor 1 (standard out) which is redirected to the file afile.

Now you could get more complicated and redirect standard out and standard error to different locations if you want to.

Something like:

# ll 1>file.log 2>file.error

The above will send file descriptor 1 (standard output) to file.log and file descriptor 2 (standard error) to file.error.

Rajeev  Shukla
Honored Contributor

Re: 2>&1

Thats right patrick,
The only difference in using 2>&1 and >afile 2>err.log is that in the first the output and error is sent synchronously to single file whereas in the other case the output is to different files asynchronously.

Cem Tugrul
Esteemed Contributor

Re: 2>&1

No any additions to Patrick's and Rajeev's
explanations...
very clear
Good Luck,
Our greatest duty in this life is to help others. And please, if you can't

Re: 2>&1

Shiv,

You asked about repercussions if you don't use it?

Cron jobs should not have any output going to standard output. Using the 2>&1 on the command line lets you log any errors and keep them from going to standard output.

Steve
Vibhor Kumar Agarwal
Esteemed Contributor

Re: 2>&1

Right,

Otherwise it might mess up your normal work.
Since cron runs in background, you should output its error to a log file.
Vibhor Kumar Agarwal
Sandman!
Honored Contributor

Re: 2>&1

Essentially 2>&1 sends the standard output and standard error stream to the same file. Take a look at the URL below for a detailed explanation of I/O re-direction.

http://wks.uts.ohio-state.edu/unix_course/intro-69.html