How To's

Redirect program output to a file using bash

2,895 views · 0 found this helpful

Was this helpful?

Problem

Send output of a program / content to a text file.

tl;dr

echo “hello world” >> hello_report.txt
./myprog.sh >> error_report.txt
./myprog > error_report.xt

Solution

Bash allows you to send the output of commands to a text file.

There are two ways you can do it. One that truncates and puts the content in the file, the other appends to the end of the file.

echo “hello world” >> hello_report.txt

This would add hello world to hello_report.txt. This will append it to the end of the file.

You could add more lines doing that.
You can also send the output of a program, like error messages to a text file

./myprog.sh >> error_report.txt

Any output from this program will be applied to the text file.

If you want to have a clear log each time, using the following bash command.

./myprog > error_report.xt
  • Updated

← All articles

Still stuck?

Our engineers answer tickets directly, 24/7.