1

When I run a shell script in bash, I push everything to a file as follows:

./script.sh >results.txt

Some of the commands in my script output to the terminal, rather than the text file.

For example, the line cmd "ssh -V" outputs to the terminal.

What can I do to get the results in the results.txt file?

2 Answers 2

2

The stderr is not redirected to the file. Most probably you need

./script.sh > results.txt 2>&1

to redirect both to results.txt. Note that

./script.sh 2>&1 >results.txt

is something different, as it redirects stdout to file and stderr to the non-redirected stdout. And of course you can substitute &1 for a different file name.

If you are using bash you will get away with

./script.sh &>results.txt

Not that in all cases the interwoven stdout/stderr are not guaranteed to be in the same order as on the console. This will work for everything, not only bash scripts.

0

Make sure you redirect all output to the text file.

  • stdout with >
  • stderr with 2>

See more here and here.

Note that the script should start with:

#!/bin/bash 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .