Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 06-09-2005, 03:11 AM   #1
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
io redirection doesn't work? 2>&1 >> logfile.log


i tried
command 2>&1 >> logfile.log
to log everything (stdout and stderr) to a logfile
i only get stdout!
i also tried
>> logfile 2>&1
the same!

command &>> logfile.log
i get an error
&> logfile.log
only gets stdout

anybody knows how it REALY works?
i searched the forum and the web and everybody thinks it's like the above methods

but they don't work

Old 06-09-2005, 04:20 AM   #2
Simon Bridge
LQ Guru
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Intreguing - I'd have thought that 2>&1 would redirect stderr into stdout - then you append stdout ... then >>file just sends stdout to the file ... so you're piping stderr to the screen and stdout to a file. To send them both you need two commands ... I'll see what google says just to check my suppositions.

<googles> I see what you mean ... however: I see it is OS/2 which redirects like this? ... did you see this one: ?

It says:
# Order of redirecting

* Method 1
[root@server /root]# cmd 2>&1 1>outfile.txt
#The above command causes the original stderr to go to stdout,
# and causes the original stdout to go to outfile.txt.
#Notice at the time that fd (file descriptor) 2 is redefined, fd 1 still pointed to stdout; therefore, fd 2 will continue to point to stdout independently of what later happens to fd 1. Basically fd 2 copies the item (or address) that fd 1 is pointing to.

* Method 2
#The following command causes both the original stdout and stderr to go to outfile.txt.
[root@server /root]# cmd 1>outfile.txt 2>&1
#Notice that 1=outfile.txt when 2 is redefined, so 2 goes to outfile.txt too (the two
# channels are combined).
So you do:
cmd 1>>logfile.log 2>&1
... of course, why not just:
cmd 2>>logfile.log 1>>logfile.log
? It's easier to read after all...

Last edited by Simon Bridge; 06-09-2005 at 04:27 AM.
Old 06-09-2005, 08:29 AM   #3
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
do you mean most bash scripting tutorials are wrong or not correct?

because says
3.6 Sample: stderr and stdout 2 file

This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.

rm -f $(find / -name core) &> /dev/null

This (thinking on the cron entry) will delete every file called 'core' in any directory. Notice that you should be pretty sure of what a command is doing if you are going to wipe it's output.

and &> didn't work too

and i never found a script which says it only works for OS/2

i tryed your solution and it worked

what did you search on google to find this?

Old 06-09-2005, 05:16 PM   #4
Senior Member
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 52
Originally posted by Thinking
do you mean most bash scripting tutorials are wrong or not correct?
certainly that page you pointed to has an example that has nothing to do with the title of the section

the point you missed was that redirection works from right to left as Simon Bridge has pointed out so expertly

so the proper command is

command >logfile.log 2>&1
command 1>logfile.log 2>&1
command 2>logfile.log 1>&2

google for a tutorial called "rute tutorial"
Old 06-10-2005, 02:35 AM   #5
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
i'm not sure if i know what you mean

1. rute tutorial looks very good ;-) thx

2. the tutorial i search (the advanced bash scripting tutorial) explained io redirection (and it didn't work)
--> i thought there was an failure in the tutorial
so, why shouldn't the bash tutorial have anything to do with my topic?

3. i tried some example on a suse distri (very new, don't know exactly the version)
i tried: cat * | grep "nothingfound" >> test.out 2>&1
bash did it, but there was nothing in test.out and cat throwed a few error messages (because it tried to cat a directory, which is what i wanted, because of the 2>&1 redirection ;-))
BUT: cat * >> test.out 2>&1 | grep "nothingfound"
works (i know it's not very usefull example)
i understand why, but it looks now as if 2>&1 works also using linux??

i thought it should "only" work on OS/2

if the above example works why doesn't work this:
./anyprogram >> test.out 2>&1
anyprogram is a self written prog (C++), which uses system command to execute some scripts, which could make error messages
but if i cat test.out there is no Error message?

is io redirection really as that complicated as it looks like (for me )

Old 06-10-2005, 04:31 AM   #6
Simon Bridge
LQ Guru
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Well - I googles the line you tried "command 2>&1" and when I saw that all the tutorials had a little tag on the end (OS/2) just quite like - I appended "linux" to the search term. The site I told about was something like second.

You have to read these things carefully - the tutorials weren't wrong as such, they just included things which don't work in linux. They didn't claim anything for linux BTW.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting: Print output to logfile, error to logfile & screen stefanlasiewski Programming 18 05-22-2008 12:47 PM
Log redirection? Filosofem Linux - General 6 11-28-2005 04:13 PM
Japanese canna won't work : Warning: &#12363;&#12394;&#28450;&#23383;&#22793;&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
BASH scripting: confused about redirection & file descriptors funkymunky Programming 1 06-07-2004 07:47 AM
Output & input redirection serotonincy Programming 3 04-12-2004 08:28 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:24 PM.

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