1833187 Members
2924 Online
110051 Solutions
New Discussion

2>&1

 
j773303
Super Advisor

2>&1

What's mean of the 2>&1 ?
Hero
7 REPLIES 7
Steven E. Protter
Exalted Contributor

Re: 2>&1

This is a output redirect.

Just sending the output someplace other than the terminal.

You'll see it a lot in crontab sends stdout and stderr where you want it.

I commonly use it in combination with mailx to get cron jobs to email people

From the book

0 is standard input (usually a keyboard, can be a file)

1 is standard output

2 is standard error

2>&1

redirect standard error to standard output.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Steven E. Protter
Exalted Contributor

Re: 2>&1

More correct definintion

Page 70 of the Raqeef manual:

2>&1 send stderr and stdout to the same file.

For me. I either direct to a regular file or to mailx -s "subject" recipient.

Sorry it took two posts. duh.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Michael Tully
Honored Contributor

Re: 2>&1

Hi,

It is output re-direction of either stdout or stderr. A very useful way of using this is in crontab.

Regards
Michael
Anyone for a Mutiny ?
Fragon
Trusted Contributor

Re: 2>&1

Hi,
First, please make sure that "1>" equal to ">", that means standard output! And "2>" means standard error!
For "2>$1" means the output of stderr just the same as stdout!
Normally, standard output is the terminal!

For example:
#ls >/tmp/dir.txt 2&>1
This will output the stdout & stderr to /tmp/dir.txt both!

-ux
Rajeev  Shukla
Honored Contributor

Re: 2>&1

Hi,
The ">" sign is used for redirecting the output.
and
2=standard error
1=standard output
0=standard input
when used with the scripts you can use either way to redirect the output and error either to standard out put or to a file.

when 2>&1 is used with a filename you are redirecting the output and error both to that file.

Cheers
Rajeev
Yogeeraj_1
Honored Contributor

Re: 2>&1

hi,

to add to the above replies,

In my crontabs, i prefer to use:
1>/home/yd/logfiles/output-myscript.crn 2>/home/yd/logfiles/error-myscript.crn

instead of:
1> /home/yd/logfiles/both-myscript.crn 2>&1

hope this helps!

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Caesar_3
Esteemed Contributor

Re: 2>&1

Hello!

Redirect the standard ERROR to the standard OUTPUT, useful for logging when you want to
save the errors.


Caesar