Bash Input Redirection

May 17th, 2008 by Mitch Frazier in

Your rating: None Average: 4.3 (3 votes)

If you use the shell you surely know about redirection:

  # echo 'hello world' >output
  # cat <output
The 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
  > EOF
A "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' >&2
This 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 and the Web Editor for linuxjournal.com.


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. 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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous's picture

Here is an example of a way

On September 4th, 2009 Anonymous (not verified) says:

Here is an example of a way to redirect command output without using a pipe.

while read line; do
echo This file: $line;
done <<EOF
$(ls)
EOF

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

Adam Backstrom's picture

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_files containing a list of files, you could grep the list:

grep mp3 < matched_files

Or do something to each file in the list:

while read line ; do
  flac "$line" && rm -f "$line"
done < matched_files

Lots more great examples are on the Useless Use of Cat Award page.

Serge van Ginderachter's picture

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

Mitch Frazier's picture

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:
   tmp    = stdout
   stdout = stderr
   stderr = tmp
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 and the Web Editor for linuxjournal.com.

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.







Read this issue