Bash Input Redirection
May 17th, 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 the System Administrator at Linux Journal.
Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Sorry, offer available in the US only. International orders, click here.
Subscribe now!
The Latest
Featured Videos
Email is one of the least private and least secure forms of communication, although few people realize this. MixMaster is one way to allow secure, anonymous communication even over the very public medium of email. This tutorial will get you started with MixMaster quickly and easily.
In case you were wondering about the fun side of Linux World Expo, we thought we'd give you a peek at our shenanigans. We at Linux Journal love what we do so much, that we can't help but have a ball wherever we go.
Recently Popular
From the Magazine
September 2008, #173
Feeling a bit like a Thermian? Never give up, never surrender! Someday, you could go from underdog to top dog. Just take a look at a few of the underdogs we highlight in this issue: Mutt, djbdns, Nginix, Gentoo, Xara and the program voted mostly likely to fail just a few years back—Firefox. If Firefox is not radical enough for you, check out Chef Marcel's column for some more alternatives. Having trouble mapping your program data to your relational database? If so, Rueven Lerner shows you some tricks in his At The Forge column.
Need to run GUI applications on your server in the next state? In his Paranoid Penguin column, Mick Bauer shows you how to do it securely. Kyle Rankin keeps hacking and slashing and shows you a few split screen secrets you may not be familiar with. Finally, we all know what happens next February, but only Doc knows what happens afterward.
Delicious
Digg
Reddit
Newsvine
Technorati







It's worth mentioning some
On May 20th, 2008 Adam Backstrom (not verified) says:
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
On May 17th, 2008 Serge van Ginderachter (not verified) says:
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
On May 17th, 2008 Mitch Frazier says:
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 the System Administrator at Linux Journal.