Loading
Home ›
Bash Input Redirection
May 17, 2008 By Mitch Frazier
in
If you use the shell you surely know about redirection:
# echo 'hello world' >output # cat <outputThe first line writes "hello world" to the file "output", the second reads it back and writes it to standard output (normally the terminal).
Then there are "here" documents:
# cat <<EOF > hello > world > EOFA "here" document is essentially a temporary, nameless file that is used as input to a command, here the "cat" command.
A less commonly seen form of here document is the "here" string:
# cat <<<'hello world'In this form the string following the "<<<" becomes the content of the "here" document.
Another less commonly seen form of redirection is redirecting to a specific file descriptor:
# echo 'Error: oops' >&2This redirects the output of the "echo" command to file descriptor 2, aka standard error. This is useful if you want to keep the error output of your scripts from contaminating the normal output when the output of your script is redirected.
These features work in bash and may not be available in other shells.
______________________
Mitch Frazier is an Associate Editor for Linux Journal.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Linux-Based X Terminals with XDMCP
- 100% disappointed with the decision to go all digital.
- Readers' Choice Awards 2011
- Parallel Programming with NVIDIA CUDA
- You Need A Budget
- Validate an E-Mail Address with PHP, the Right Way
- The Linux powered LAN Gaming House
- The Linux RAID-1, 4, 5 Code
- Python for Android
- Gnome3 is such a POS. No one
2 hours 49 min ago - Gnome 3 is the biggest POS
3 hours 24 sec ago - I didn't knew this thing by
9 hours 4 min ago - Author's reply
12 hours 29 min ago - Link to modlys
13 hours 35 min ago - I use YNAB because of the
13 hours 47 min ago - Search
18 hours 50 min ago - Question
19 hours 13 min ago - for the record
19 hours 15 min ago - That's disappointing. Thanks
21 hours 39 min ago





Comments
Here is an example of a way
Here is an example of a way to redirect command output without using a pipe.
Very useful. You could, for example, substitute ls for some program to query a database or sed commands for processing some files
I am not sure if this will run on old versions of bash though
It's worth mentioning some
It's worth mentioning some more examples of input redirection. For a file
matched_filescontaining a list of files, you could grep the list:grep mp3 < matched_filesOr do something to each file in the list:
Lots more great examples are on the Useless Use of Cat Award page.
swap STDOUT and STDERR
swap STDOUT and STDERR: "3>&1 1>&2 2>&3"
as in:
(/usr/bin/$COMMAND $PARAM 3>&1 1>&2 2>&3 | grep -v $uninteresting_error ) 3>&1 1>&2 2>&3
Grep stderr
In a bit more detail:
-
3>&1 - moves file descriptor 1 (aka stdout) to file descriptor 3.
-
1>&2 - moves file descriptor 2 (aka stderr) to file descriptor 1.
-
2>&3 - moves file descriptor 3 to file descriptor 2 (aka stderr).
Similar to: and thereby swapping the standard out and the standard error. Which then allows the stderr (rather than stdout) to be piped into grep.Mitch Frazier is an Associate Editor for Linux Journal.
To elaborate further
3>&1 redirects output sent to 3 to stdout,
1>&2 redirects output sent to stdout to stderr,
and 2>&3 redirects output sent to stderr to 3.
If you just want to include stderr output in your stdout along with the existing stdout output, you can just use 2>&1