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 Pitfalls

114 pointsby zanokover 11 years ago

14 comments

brandonbloomover 11 years ago
Maybe some controversial advice: Go ahead, fall in these pits.<p>I write my fair share of shell scripts and I&#x27;ve hit practically every one of these snags in the past. However, for the majority of tasks I perform with bash, I genuinely don&#x27;t care if I support spaces in filenames, or if I throw away a little efficiency with a few extra sub-shells, or if I can&#x27;t test numbers vs strings or have a weird notion of booleans.<p>Your scripts are going to have bugs. The important question is: What happens when they fail?<p>Are your scripts idempotent? Are they audit-able? Interruptible? Do you have backups before performing destructive operations? How do you verify that they did the right job?<p>For example, if your shell scripts operate only on files under version control, you can simply run a diff before committing. Rather than spent a bunch of time tracking down a word expansion bug, you can simply rename that one file that failed to not include a space in its name.
评论 #6791208 未加载
评论 #6790466 未加载
comexover 11 years ago
The Unix shell may be a highly powerful interactive programming environment, but it&#x27;s sure hard to think of anything that comes anywhere close to <i>sucking</i> as badly. With the shell and the standard Unix commands, some things that are hard in other languages are easy, and most of the things that are easy in other languages are hard to impossible... I&#x27;d love to see a clean slate replacement for the shell that still feels Unix-like and retains most of its existing benefits.<p>(I suspect PowerShell would be a good environment to take design cues from or even port, but I&#x27;ve never used it so I can&#x27;t say for sure.)
评论 #6791134 未加载
评论 #6790869 未加载
评论 #6793241 未加载
评论 #6791808 未加载
评论 #6795932 未加载
评论 #6791295 未加载
mlacitationover 11 years ago
I have a mirror of this site. wooledge.org runs off of greycat&#x27;s (#bash on freenode) home DSL connection:<p><a href="http://bash.cumulonim.biz/BashPitfalls.html" rel="nofollow">http:&#x2F;&#x2F;bash.cumulonim.biz&#x2F;BashPitfalls.html</a>
评论 #6790294 未加载
评论 #6790256 未加载
Sprintover 11 years ago
Apparently a mod changed the URL (to relieve a not so powerful host). The original URL (and thus the one you should be bookmarking&#x2F;remember) was <a href="http://mywiki.wooledge.org/BashPitfalls‎" rel="nofollow">http:&#x2F;&#x2F;mywiki.wooledge.org&#x2F;BashPitfalls‎</a><p><a href="http://bash.cumulonim.biz/BashPitfalls.html" rel="nofollow">http:&#x2F;&#x2F;bash.cumulonim.biz&#x2F;BashPitfalls.html</a> is a mirror, see mlacitation&#x27;s comment.
PhasmaFelisover 11 years ago
So why do we put up with classic command line tools in general that are so full of horrible, counterintuitive pitfalls? Is it just tradition? Backwards compatibility?<p>The &quot;Unix should be hard&quot; crew has gotten a lot quieter in the last ten years with the rise of Ubuntu and other relatively user-friendly distros, but I feel like there&#x27;s still an underlying current of elitism there; people are proud of mastering these bizarre, arcane methods, and they&#x27;re offended that someone else might be able to accomplish just as much without doing half as much work.
评论 #6791286 未加载
secureover 11 years ago
Here’s my personal favorite shell pitfall, which was the last drop to make me start recommending _against_ using shell except for very very narrow niche use cases: <a href="https://plus.google.com/+MichaelStapelberg/posts/YLarC7WPVQB" rel="nofollow">https:&#x2F;&#x2F;plus.google.com&#x2F;+MichaelStapelberg&#x2F;posts&#x2F;YLarC7WPVQB</a>
dj-wonkover 11 years ago
Would someone recommend an automatic bash style checker, such as a &#x27;linter&#x27;? Perhaps something along the lines of Chef&#x27;s Food Critic? <a href="http://acrmp.github.io/foodcritic/" rel="nofollow">http:&#x2F;&#x2F;acrmp.github.io&#x2F;foodcritic&#x2F;</a>
评论 #6793445 未加载
joshbaptisteover 11 years ago
Ah yes, the ultimate reference from Freenode #bash, after you learn from Greycat&#x27;s wiki you won&#x27;t simply Google&#x2F;DDG &quot;bash tutorial&quot; again and just head straight here.
ak217over 11 years ago
I find it sad and amusing that we&#x27;re writing a ton of mission-critical code in this language that has an incredible number of obscure quirks. Yes, most of these pitfalls are directly connected to the semantics of Unix, but I wish someone made a concerted effort to get rid of them in an otherwise evolutionary way.
druiidover 11 years ago
Bash is for most things, one of the easier languages I have dealt with, being even beyond python, etc. That is though, for shell type scripting.<p>There&#x27;s many problems with it, but the only one I&#x27;ve run into that keeps it from being more useful is that there are no multi dimensional arrays built-in. There are super hacky ways I have seen them implemented, but by default it&#x27;s something I basically never am able to turn to when scripting in bash and have to turn to other languages, even when the particular task I was working with would be mostly simpler in bash.<p>That said, there are associative arrays in bash these days.
Aardwolfover 11 years ago
The following does not work on files with spaces according to the article:<p><pre><code> for i in $(ls *.mp3); do some command $i done </code></pre> So does that mean, that &quot;for&quot; will do something per <i>word</i> of the output of $, rather than per <i>line</i> of output of it?<p>What to do if I want to do something for every line? What for example if I really want the output of ls, find (or any other command you can put int he $()) and loop through that line per line, even if some output has spaces?<p>Thanks.
评论 #6790875 未加载
评论 #6790895 未加载
anon4over 11 years ago
I know someone will sooner or later propose that we ban spaces and special characters in names. Let me just put my two cents forward.<p>We should absolutely ban special characters from names. Specifically, all whitespace, the colon, semicolon, forward slash, backward slash, question mark, star, ampersand, and whatever else I&#x27;m missing that will confuse the shell. Also files cannot start with a dash.<p>However, people should be able to name files with these characters. So I propose that these characters in filenames be percent-encoded like they would be in a URL. Specifically, the algorithm should be<p>1. Take the file name and encode it as UTF-8. Enforce some sort of normalization.<p>2. Substitute each problematic byte with equivalent percent-encoded form. This does not touch bytes over 0x80 - they are assumed non-problematic.<p>3. Write the file in the file system under that name.<p>4. When displaying files, run the algorithm in reverse.<p>In the general case files like &quot;01 - Don&#x27;t Eat the Yellow Snow.mp3&quot; would simply become 01%20-%20Don&#x27;t%20Eat%20the%20Yellow%20Snow.mp3 in the filesystem and cause absolutely no further problems. To make it completely backwards-compatible we should also add the following rule: If a filename includes a problematic byte or a percent-encoded byte higher than 0x80, then it is assumed to be raw and will not undergo percent decoding.<p>Basically, I propose that every program which receives free text input for a file name percent-encode the filenames before writing them to the filesystem and decode them for display. Everything else remains unchanged.<p>Why this will not work:<p>Requiring programmers to keep track of two filenames instead of just one is rather a lot of work. File APIs will have to take both encoded and non-encoded forms and encode the non-encoded form, creating problems when people inadvertently use the wrong function with a name, either double-encoding it or not encoding it and leading to &quot;this file does not exist&quot; errors.<p>It will be possible to create two files with different names on disk which are nonetheless shown with the same name to the user.<p>Why it is ugly:<p>We&#x27;re taping over a deficiency of an ancient language by inflicting pain on programmers.<p>Double-encoded filenames? MADNESS.<p>Why I like it:<p>I&#x27;ll be able to have ?, * and : in filenames in windows.<p>My shell scripts will be much simpler.<p>What do you guys think?
评论 #6792051 未加载
Nick_Cover 11 years ago
<p><pre><code> for arg </code></pre> instead of<p><pre><code> for arg in &quot;$@&quot; </code></pre> is gold. That is going straight to the pool room.
Timmmmbobover 11 years ago
1. Using bash.