Follow me on Twitter for my latest adventures!
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:
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.



Facebook
more
LinkedIn
Google+
GitHub

Comments
You missed a couple:
Turn off input output (throw in to the background for fun too):
>&- <&- 2>&- &
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.
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')".
Oops, this example is wrong, you don't use $(...).
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.
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.
Hi!
Peter, I am glad there are such talents as you are!
....its very fascinating article with very much useful information
Leave a new comment