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.

More Things I Wish I’d Known About Bash

150 pointsby jesperhtover 7 years ago

17 comments

ckuehlover 7 years ago
This is a somewhat dangerous pattern for picking temporary files (from #8):<p><pre><code> $ NEWFILE=&#x2F;tmp&#x2F;newfile_${RANDOM} $ touch $NEWFILE </code></pre> The problem is that any user on the box can create files under &#x2F;tmp. An attacker can set up a bunch of symlinks like &#x2F;tmp&#x2F;newfile_1, ..., &#x2F;tmp&#x2F;newfile_99999 pointing to a file owned by your user. When your script then writes into this temporary file, you&#x27;ll write through the symlink and clobber one of your own files. Especially dangerous if root :)<p>This has been a historic source of software vulnerabilities (often with the PID used instead as the guessable component instead of random, though). One recommended alternative is to use the `mktemp` command instead.
评论 #16200621 未加载
评论 #16200157 未加载
评论 #16205793 未加载
guhcamposover 7 years ago
Portuguese speakers have counted for a long time with this gem, simply the best Bash doc&#x2F;cheat sheet I&#x27;ve ever seen on the web. This is probably my oldest bookmark still relevant after 15 years.<p>It&#x27;s in portuguese and I&#x27;m not sure if there&#x27;s an official translation, yet it&#x27;s easy enough to decipher if you know bash, and Google Translate will do a pretty decent job.<p>I gift you &quot;Aurelio&#x27;s Swiss Army Knife of the Bash Shell&quot; - <a href="http:&#x2F;&#x2F;aurelio.net&#x2F;shell&#x2F;canivete&#x2F;" rel="nofollow">http:&#x2F;&#x2F;aurelio.net&#x2F;shell&#x2F;canivete&#x2F;</a>
评论 #16199982 未加载
评论 #16199589 未加载
评论 #16201774 未加载
_kst_over 7 years ago
As I mentioned in my comment on the article, setting $TMOUT if you want a timeout on a &#x27;read&#x27; command is unnecessary and unclear. Just use &quot;read -t&quot;:<p><pre><code> read -t 5 foo || foo=&#x27;No reply&#x27; </code></pre> Setting $TMOUT affects all following &#x27;read&#x27; commands. Also, setting $TMOUT in an interactive shell sets a timeout for a response to the primary prompt, terminating the shell if the user doesn&#x27;t respond in time.
gvalkovover 7 years ago
Fiy, heredocs also have a variant that strips all leading tab characters. Quoting from `man 1 bash`:<p><pre><code> If the redirection operator is &lt;&lt;-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here- documents within shell scripts to be indented in a natural fashion.</code></pre>
评论 #16201131 未加载
hyperpapeover 7 years ago
I imagine most uses of random in Bash don&#x27;t need to be that robust, but it might be worth mentioning that ${RANDOM}${RANDOM} has a lot of bias as a random number generator.
评论 #16205807 未加载
评论 #16199471 未加载
antoineMoPaover 7 years ago
&quot;Sigh. Hit ‘up’, ‘left’ until at the ‘p’ and type ‘e’ and return.&quot;. My solution for this one would be :<p>UP CTRL-A RIGHT RIGHT e<p>Which needs less thinking and 6 keystrokes instead of 8.
评论 #16200410 未加载
评论 #16199599 未加载
评论 #16205816 未加载
SteveNutsover 7 years ago
I&#x27;m so happy that Bash is getting the love it deserves.
评论 #16199212 未加载
nerdponxover 7 years ago
Just another plug for Zsh: it has all of these features and then some.<p>- Safe-by-default parameter expansion: no word splitting unless you ask for it, even if you don&#x27;t quote the expansion.<p>- Ability to use histoy expansions (like !!:gs&#x2F;foo&#x2F;bar) on parameter expansions, meaning &quot;${foo:A:h}&quot; is equivalent to &quot;$(dirname $(realpath $foo))&quot;<p>- Much better array support, including both integer-indexed and associative arrays<p>- A built-in CLI option parser that&#x27;s pretty robust (&quot;zparseopts&quot;)<p>- Lazy-loaded functions<p>- Floating-point arithmetic
评论 #16199664 未加载
评论 #16199481 未加载
awllover 7 years ago
<i>Sigh. Hit ‘up’, ‘left’ until at the ‘p’ and type ‘e’ and return.</i><p>One could also use &lt;c-p&gt;,&lt;c-a&gt; and &lt;c-f&gt; to achieve the same result much quicker.
评论 #16199328 未加载
评论 #16199474 未加载
评论 #16199304 未加载
dorfsmayover 7 years ago
Interesting, most of these are from korn shell and well documented in &quot;The korn shell command and programming language&quot; by Bolsky and Korn.
iamdaveover 7 years ago
Well that first example with the mistyped grep command just blew my mind.
partycoderover 7 years ago
I agree with most of it except for:<p><pre><code> ${RANDOM}${RANDOM} </code></pre> A preferred way would be<p><pre><code> od -vAn -N4 -tu4 &lt; &#x2F;dev&#x2F;urandom </code></pre> &#x2F;dev&#x2F;urandom gives you random bytes, od dumps them in different formats (e.g: hex, octal, decimal and such).<p>This takes 4 random bytes and outputs them as a 4 byte unsigned int.
评论 #16200698 未加载
coroxoutover 7 years ago
I use pushd and popd every day. If you forget what&#x27;s in the stack you can see what&#x27;s in it by using the &quot;dirs&quot; command.<p>You can also use pushd -n (where n is a number, although I usually end up needing trial and error to get the right one) to rotate the list of dirs without removing any from the stack - useful if the list has more than 2 directories, or 2+ directories you need to switch between repeatedly.<p>pushd and popd also work out of the box in Windows command prompt, although you don&#x27;t get &quot;dirs&quot; or any fancier options like in bash, just push and pop on a plain old stack.
zwischenzugover 7 years ago
Previous post discussion: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=16084763" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=16084763</a>
sliover 7 years ago
<p><pre><code> ^x^y^ </code></pre> Is that third caret necessary? I&#x27;ve never had to include it. Although that may be zsh taking a shortcut on bash syntax.
评论 #16210798 未加载
7kmphover 7 years ago
Things I Wish I’d Known About Bash: don&#x27;t use it for script
cryptonectorover 7 years ago
But you really should know that set -e is broken.
评论 #16205780 未加载