I have a shell script that I should preferably not be editing but that I would have to use the output of. Doing ./SomeScript.sh -d displays the output I need on the shell.

However, I get an empty file if I try to redirect the output.

./SomeScript.sh -d > temp 
cat temp

After checking Why redirecting output sometimes produces an empty file?, I have also tried echo $(./SomeScript.sh -d) but the command prints nothing.

Initially I was trying to grep a line from the output, but after realising that nothing was returned, I tried to redirect the output to a file instead and realised this problem. Any idea why this might be happening?

  • Is there an empty line at the end of the script? What does ./SomeScript.sh -d >> temp do? – Grace Thompson Mar 11 at 3:56
  • If you're talking about the temp script where the output was redirected to, yes, there's one empty line – IceTea Mar 11 at 3:57

If ./SomeScript.sh -d > temp results in an empty file (and yet you can see the output on the screen), that means that the program is maybe printing to stderr instead of stout. So, try redirecting its stderr too -- i.e., use this:

./SomeScript.sh -d >temp 2>&1

Or you could send the outputs to two separate files:

./myScript  1>outtemp 2>errtemp 
  • This might be the solution for some others so thanks for the answer, but it's not the case here. – IceTea Mar 12 at 3:08
  • Have you looked inside that script to see if it's doing some funny stuff? Can you paste it someplace where we can see it? – Chota Mar 12 at 3:13
  • it's very long and calls other functions, but one possible cause might be that the echo commands are being passed to named pipes (tbh, I'm not familiar with named pipes) instead of being directly invoked by the script – IceTea Mar 12 at 4:24


./SomeScript.sh -d >> temp

this will append each line of the script, rather than write and overwrite line by line.

Edit: try

./SomeScript.sh -d &>> temp

if you want to redirect stderr as well as stdout.

  • Unfortunately this didn't resolve the issue – IceTea Mar 11 at 4:10
  • Hmmm, it worked on my machine (famous last words, I know). Could you post the contents of SomeScript.sh perhaps, if it's not too long? – Grace Thompson Mar 11 at 4:14
  • I know sharing the script would really help, but unfortunately it's really long and proprietary so I won't be able to – IceTea Mar 11 at 6:11
  • 2
    If it's "proprietary", then you're in the wrong place. We can't tell you why something we can't see is not working. – Chota Mar 13 at 19:49

script -c "./SomeScript.sh -flags" temp

This command worked for me but I'm not too sure why either.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.