william
william

Reputation: 55

bash wont output to the file even though it has a '>'?

i have a bash script that is similar

cd dir
echo $1 > foo.foo
cmd 2> results.foo > results.foo

but nothing gets sent to results.foo. foo.foo gets perfect data

 cat results.foo

nothing

cat foo.foo

Upvotes: 0

Views: 102

Answers (1)

Ljm Dullaart
Ljm Dullaart

Reputation: 4969

If you do

cmd > tesults.foo

the file is re-created with the output that is on STDOUT. If you do

cmd 2> tesults.foo 

the file is re-created with STDERR.

If you do both, as you did,

cmd 2> results.foo > results.foo

the first of the STDIN or STDOUT will recreate the file. Then the second of the two streams will recreate the file. So it will depend on which one is last what you will see in the file.

There are of course simple solutions (you're not the only one that needs this): make sure that both streams go into the result.foo (put the StDOUT output of cmd in result.foo and put the STDERR output(2) in the location where STDOUT(1) goes):

cmd > results.foo 2>&1

Or use different files for STDOUT and STERR:

cmd > results.out 2> results.err

Upvotes: 1

Related Questions