More Bash Redirections
Everybody's seen redirection in bash commands, that's pretty common, but bash also allows you to define redirections when you define functions. This causes the redirections to be evaluated/executed whenever the function is called. This feature doesn't really give you any new features, just another way to express existing features.
The syntax for this is simple, you simply append the redirections onto the end of the function defintion:
function testy()
{
...
} < testy.in > testy.out 2> testy.err
Now whenever the function testy is called its input will come from testy.in, its output will go to testy.out and any errors will go to testy.err.
Since the redirections are evaluated when the function is called the redirection(s) can use variables and the variables are also evaluated when the function is called. So you could do something like this:
#!/bin/bash
function testy()
{
echo testy westy
} >$out
out=jj1
testy
out=jj2
testy
This causes the output of the function to go to a different file with each call. The first call's output goes to jj1 and the second's to jj2:
$ bash j.sh; more jj?
::::::::::::::
jj1
::::::::::::::
testy westy
::::::::::::::
jj2
::::::::::::::
testy westy
As I mentioned earlier, there's not any real new capability here, you could also accomplish the same thing this way:
#!/bin/bash
function testy()
{
echo testy westy
}
testy >jj1
testy >jj2
One possible use of this feature might be to put all your code inside a main function and then redirect it's error output so that you make sure it always gets captured:
#!/bin/bash
log=kk
function error()
{
echo "$*" >&2
}
function testy()
{
error testy westy
}
function testy2()
{
error testy2 westy2
}
function main()
{
testy
testy2
} 2>$log
main
Running this produces:
$ bash k.sh ;cat kk
testy westy
testy2 westy2
Since bash also allows redirection to be included on {...} blocks/lists you could accomplish the same thing with the following:
#!/bin/bash
log=mm
function error()
{
echo "$*" >&2
}
function testy()
{
error testy westy
}
function testy2()
{
error testy2 westy2
}
{
testy
testy2
} 2>$log
You can also use redirections on (...) blocks/lists but that causes the commands to be executed in a sub-shell, which isn't necessary here and which may cause problems since the sub-shell is a different process.
If you're wondering if you can override the redirections in the actual call, the answer is no. If you try to override them all that happens is that redirections in the call are executed/evaluated first then the ones in the function definition replace them. For example, in the following:
#!/bin/bash
function testy()
{
echo testy westy
} >nn1
testy >nn2
The output file specified in the call (nn2) gets created but nothing gets written to it.
Also note that pipe "redirections" do not work, but here documents do:
#!/bin/bash
function testy()
{
while read line
do
echo $line
done
} <<EOF
hello
there
EOF
testy
Running this produces:
$ bash o.sh
hello
there
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
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
2 hours 2 min ago - Reply to comment | Linux Journal
2 hours 19 min ago - Favorite (and easily brute-forced) pw's
4 hours 10 min ago - Have you tried Boxen? It's a
10 hours 2 min ago - seo services in india
14 hours 33 min ago - For KDE install kio-mtp
14 hours 34 min ago - Evernote is much more...
16 hours 34 min ago - Reply to comment | Linux Journal
1 day 1 hour ago - Dynamic DNS
1 day 1 hour ago - Reply to comment | Linux Journal
1 day 2 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
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
an adjustment
Just in case the script is interrupted with a Ctrl-C or a pipe overflow, two more signals could be treated by the trap block,
trap "
...
" EXIT SIGINT SIGPIPE
Please Help!
Um hi.
I've actually been wanting to use Linux Ubuntu, but I've been having trouble and don't really have anyone to help me.
This guy at school told me to use Linux Ubuntu and he said it's a really good program and gave me a CD that says "Ubuntu Warty". He said its the same as using a Mac. I put it in and double click on it and can't get it to work right.
I also try to go to Start/Add new programs/ and still can't figure it out.
I get really tired of XP and everything and really want to try something new.
Sorry I don't know much about computers.
more redirects