Bash Shell Output Redirection

English: Babylonia spirata shell. Français : C...

Shell (Photo credit: Wikipedia)

The Bash shell is one of the best parts of the user environment on a Linux system. An essential shell function is the ability to run a command and redirect the output to a file.

Programmers will be familiar with command output being split into two parts, the standard output (stdout), and the standard error (stderr) streams. In both Linux and Windows command-line shells, these output streams are also referred to by number: 1 for stdout and 2 for stderr. The default output stream is “1” and is implied if you don’t specify a number.

You can redirect each stream to a separate file, or you can combine both into a single stream and redirect to only one file. I keep needing to look up the syntax for combine and redirect because in bash only one order will work. Here are some examples:

# stdout (1) to one file and stderr (2) to another
somecommand >out.txt 2>err.txt

# stderr (2) goes to same place as stdout (1)
somecommand >both.txt 2>&1

If you would like to see output on the screen and save the same output in a file, check out the standard “tee” command. Cheers! 🙂

About notesbytom

Keeping technology notes on to free up my mind to solve new problems rather than figuring out the same ones repeatedly :-).
This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s