BASH redirection reference

Posted on Updated on

#!/bin/bash

# redirect stdout to file
cmd > file
# redirect stderr to file
cmd 2> file
# redirect both stderr and stdout to file
cmd >& file
# pipe cmd1's stdout to cmd2's stdin
cmd1 | cmd2
# pipe cmd1's stdout and stderr to cmd2's stdin
cmd1 2>&1 | cmd2
# print cmd1's stdout to screen and also write to file
cmd1 | tee file
# print stdout and stderr to screen while writing to file
cmd1 2>&1 | tee file
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s