Bash Input Redirection
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- RSS Feeds
- Introduction to MapReduce with Hadoop on Linux
- Weechat, Irssi's Little Brother
- What the author describes
1 hour 16 min ago - Reply to comment | Linux Journal
5 hours 26 min ago - Reply to comment | Linux Journal
6 hours 11 min ago - Didn't read
6 hours 22 min ago - Reply to comment | Linux Journal
6 hours 27 min ago - Poul-Henning Kamp: welcome to
8 hours 37 min ago - This has already been done
8 hours 38 min ago - Reply to comment | Linux Journal
9 hours 23 min ago - Welcome to 1998
10 hours 11 min ago - notifier shortcomings
10 hours 35 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



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