TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Bash redirections cheat sheet

93 pointsby johndcookover 12 years ago

14 comments

dllthomasover 12 years ago
One thing worth noting is that "redirection of streams" is a poor metaphor.<p>If I have two streams,<p><pre><code> 1 ---------- 1' 2 ---------- 2' </code></pre> and I redirect the 1&#62;2<p><pre><code> 1 ---- \ 2 ---------- 2' </code></pre> it seems to work okay.<p>But if you have three,<p><pre><code> 1 ---------- 1' 2 ---------- 2' 3 ---------- 3' </code></pre> and try "redirecting 1 to 2 then 2 to 3", the natural reading would be<p><pre><code> 1 - \ 2 ----- \ 3 ---------- 3' </code></pre> But that's not what you get, because you're not "redirecting streams" - you're copying file descriptors. This is partially fixed by informing people to do the operations backwards, but there are corner cases where that breaks down, and "copying file descriptors in the order they appear on the line" never does because that's genuinely what's going on.<p><pre><code> 1 -&#62; 1' 2 -&#62; 2' 3 -&#62; 3' </code></pre> foo 1&#62;2 2&#62;3<p>This is "run foo copying fd 2 to 1 and 3 to 2"<p>which gives<p><pre><code> 1 -&#62; 2' 2 -&#62; 3' 3 -&#62; 3'</code></pre>
评论 #4503078 未加载
gwillenover 12 years ago
I think I previously had all the pieces required to do this, but only just figured out that it worked:<p><pre><code> Glenn-Willens-MacBook-Pro:~ gwillen$ exec 3&#60;&#62; /dev/tcp/&#60;website&#62;/80 Glenn-Willens-MacBook-Pro:~ gwillen$ echo "GET / HTTP/1.0" &#62;&#38;3 Glenn-Willens-MacBook-Pro:~ gwillen$ echo "" &#62;&#38;3 Glenn-Willens-MacBook-Pro:~ gwillen$ cat &#60;&#38; 3 HTTP/1.1 200 OK Date: Mon, 10 Sep 2012 21:43:33 GMT Server: Apache Last-Modified: Sun, 06 Sep 2009 18:44:42 GMT ETag: "30525-0-472ed20537a80" Accept-Ranges: bytes Content-Length: 0 Connection: close Content-Type: text/html Glenn-Willens-MacBook-Pro:~ gwillen$</code></pre>
dllthomasover 12 years ago
One thing missing is &#60;&#60;- heredocs, which strip (some) leading whitespace. It lets you keep up your indentation in a shell script without passing a bunch of whitespace to the commands.
shabbleover 12 years ago
I've found<p><pre><code> cmd &#60;(another_cmd) </code></pre> to be particularly useful in the past for pushing generated data into something that doesn't accept - as an alias for STDIN. I'm not sure if it's a different way of expressing one of the listed items though; iirc it creates a temporary fifo
评论 #4502769 未加载
bajsejohannesover 12 years ago
I've always done<p><pre><code> (cmd 2&#62;&#38;1) &#62; file </code></pre> to redirect stderr and stdout to a file. I always thought the parenthesis were necessary, but according to this I can just do<p><pre><code> cmd &#62; file 2&#62;&#38;1 </code></pre> It seems very unintuitive, though. Does anyone know the rationale behind ordering of redirects?<p>(That said, I'm going to use 'cmd &#38;&#62; file' that I just learned from now on)
评论 #4502282 未加载
评论 #4502450 未加载
评论 #4502308 未加载
评论 #4504377 未加载
dredmorbiusover 12 years ago
The network pseudo filehandles (/dev/tcp/&#60;host&#62;/&#60;port&#62; and /dev/udp/&#60;host&#62;/&#60;port&#62;) are explicitly disabled in certain distros (notably Debian). Ubuntu appears to support it.<p>Though not limited to just shell redirections, one of the true masters of bash (and zsh) is Larry Peek whose written/given multiple books, articles, and presentations on the topic.
评论 #4502779 未加载
lurielover 12 years ago
I can never remember the (ba)sh redirection syntax, and I used it for decades.<p>Rc on the other hand is much more clean: <a href="http://rc.cat-v.org" rel="nofollow">http://rc.cat-v.org</a>
AceJohnny2over 12 years ago
the "cmd &#60;&#60;EOL ..\n..\n EOL" part has been mystical to me for ages, especially as I didn't know what to google to find out. I recently learned it was called a "Here Document". <a href="http://en.wikipedia.org/wiki/Here_document" rel="nofollow">http://en.wikipedia.org/wiki/Here_document</a><p>For extra credit, try using a "Here Document" as input for 'echo' and 'cat'. Explain the results.
评论 #4509028 未加载
评论 #4502125 未加载
darkstalkerover 12 years ago
The PNG version is rendered with subpixel antialiasing, which is kinda annoying to see. Besides that, awesome job.
评论 #4503060 未加载
zwilliamsonover 12 years ago
Where is EOF? I use that a lot in my Bash scripts. I think it should be on this cheat sheet.
dllthomasover 12 years ago
I like the mention of PIPESTATUS - $PIPESTATUS[*] is at the start of my PS1
michaelhoffmanover 12 years ago
Thanks! Much more useful than the other recent alphabetic cheat sheets.
truskoover 12 years ago
Great! Thanks for sharing, very helpful.
methoddkover 12 years ago
Very useful! Thank you for this.