Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

13
  • 28
    As I mention in my answer, see unix.stackexchange.com/questions/68694/… for details. Do notice the question — “Why does my shell script choke?”. The most common problem (from years of experience on this site and elsewhere) is missing double quotes. “Always use double quotes” is easier to remember than “always use double quotes, except for these cases where they aren't necessary”. Commented May 24, 2014 at 8:25
  • 19
    Rules are difficult to understand for beginners. For instance, foo=$bar is OK, but export foo=$bar or env foo=$var are not (at least in some shells). An advise for beginner: always quote your variables unless you know what you're doing and have a good reason not to. Commented May 24, 2014 at 17:05
  • 7
    @StevenPenny Is it really more correct? Are there reasonable cases where quotes would break the script? In situations where in half cases quotes must be used, and in other half quotes may be used optionally - then a recommendation "always use quotes, just in case" is the one that should be thought, since it's true, simple and less risky. Teaching such lists of exceptions to beginners is well known to be ineffective (lacking context, they won't remember them) and counterproductive, as they'll confuse needed/unneeded quotes, breaking their scripts and demotivating them to learn further. Commented May 25, 2014 at 8:20
  • 9
    My $0.02 would be that recommending to quote everything is good advice. Mistakenly quoting something that doesn't need it is harmless, mistakenly failing to quote something that does need it is harmful. So, for the majority of shell script authors who will never understand the intricacies of when exactly word splitting occurs, quoting everything is much safer than trying to quote only where necessary. Commented May 25, 2014 at 17:03
  • 6
    @Peteris and godlygeek: "Are there reasonable cases where quotes would break the script?" It depends on your definition of "reasonable". If a script sets criteria="-type f", then find . $criteria works but find . "$criteria" doesn't. Commented Aug 28, 2014 at 19:06