Hey guys! A few weeks ago I wrote an article called All About Bash Redirections. It explains all the possible bash redirections with illustrations. I thought it would be a great idea to make a cheat sheet that summarizes all these redirections. So I did.

Here is the bash redirections cheat sheet:


Download PDF | Download PNG | Download TXT | Download TEX

If you want to learn how each one of these redirections work, read my article All About Bash Redirections!

Found a mistake or want to contribute to this cheat sheet? Fork it on github!

Enjoy!

PS. I've created a dozen different cheat sheets. Take a look at my other cheat sheets about awk, ed, sed, perl, screen, more bash, gnu coreutils, util-linux, and many others.

Comments

Dave Permalink
September 10, 2012, 18:15

You missed a couple:

Turn off input output (throw in to the background for fun too):
>&- <&- 2>&- &

lava Permalink
September 11, 2012, 12:44

Warning: closing descriptors could cause some apps to fail, so be aware.
+-----------------------------------------------
|$ exec >&-
|-bash: echo: write error: Bad file descriptor
|-bash: echo: write error: Bad file descriptor
|$ ls
|ls: write error: Bad file descriptor
|-bash: echo: write error: Bad file descriptor
|$ echo $? >&2
|2
|-bash: echo: write error: Bad file descriptor
|-bash: echo: write error: Bad file descriptor
|

As you can see, not only error messages have been printed to the console, but ls also returned an error (2). This could be essential for scripts and I personally would recommend redirection to /dev/null.

Nick Coleman Permalink
September 11, 2012, 05:04

Just to note that the Here String ("<<<") is not just for text strings, but is subject to normal Bash word expansion. This means you can do things like cat <<< "$(/usr/bin/date)".

I use this construction nearly every day. A common use case for me is with the at command, which doesn't accept command line input for some silly reason. I work around it by doing: at 5pm <<< "$(gxmessage 'Take steak out of freezer')".

Nick Coleman Permalink
September 11, 2012, 08:25

Oops, this example is wrong, you don't use $(...).

lava Permalink
September 11, 2012, 13:03

I think, your cheatsheet should contain warnings for dangerous bash-isms.
Those ones: <<<, >(), named descriptors, PIPESTATUS, |& (this one you already mentioned).

At least, there should be a warning to use explicitly #!/bin/bash instead of #!/bin/sh for scripts, because some distributions use dash by default for /bin/sh.

iteratee Permalink
September 12, 2012, 07:28

Of those, only PIPESTATUS and |& are actually bash-only. The title of the cheatsheet contains "Bash". Anybody who assumes that means POSIX deserves to have things broken, as do users of horrible pointless shells such as Dash.

For various reasons, |& should probably be discouraged even in Bash scripts.

Albert Nome Permalink
May 15, 2013, 06:05

Hi!

Peteris, I am glad there are such talents in Latvia as you are! I love Riga and Latvian people, you guys are the smartest in the Baltic region :)

abir Permalink
August 28, 2013, 10:16

....its very fascinating article with very much useful information

Viviana Permalink
January 22, 2015, 11:00

Hi!

Peteris, I am glad there are such talents in Latvia as you are! I love Riga and Latvian people, you guys are the smartest in the Baltic region :)

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "unix_342": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements