<div dir="ltr"><div dir="ltr" class="gmail_attr">On Sun, 15 Sep 2019 at 11:01, William Park via talk <<a href="mailto:talk@gtalug.org">talk@gtalug.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sat, Sep 14, 2019 at 10:02:46AM -0400, D. Hugh Redelmeier via talk wrote:<br>
> | From: William Park via talk <<a href="mailto:talk@gtalug.org" target="_blank">talk@gtalug.org</a>><br>
> <br>
> | Hmm, minor personal irritant about variable assigment:<br>
> |     var=$(...)<br>
> | is enough.  You don't need to do<br>
> |     var="$(...)"<br>
> <br>
> I think that getting quotes right in sh is hard.  I support the<br>
> simplest rule that works:<br>
> <br>
> Always double-quote a $ thing UNLESS it must not be quoted.<br>
> <br>
> The case you point out may be quoted, so I would quote it.<br>
<br>
Extra quotes distract your eyes, makes it difficult to read or spot a<br>
bug.  It's like always carrying an opened umbrella (because you can),<br>
when you should do it only when it rains.<br clear="all"></blockquote><div><br></div><div>We covered this the first time I posted about the script - clearly there are some strongly held opinions on the subject.  I'm going to state mine once.</div><div><br></div><div>If you have a variable called "a" and another called "ab" and you type "$ab" the shell script, you'll get the contents of variable "ab" not the contents of variable "a" followed by the letter "b".  So I _always_ put braces around my variable names - even though it's a pain to type and not particularly beautiful.  It's syntactically correct and I won't ever get an unexpected error.</div><div><br></div><div>My thinking is the same about quoting.</div><div><br></div><div>    filename="/var/log/apache.log"</div><div>    cat $filename</div><div><br></div><div>(Never mind it's a contrived example.)  That works just fine.  I didn't brace or quote $filename.  And - as I said last time - everything goes swimmingly right up until someone edits your file, or gives it some input such that the filename variable includes a space.  And then - if you're lucky - you get a "file not found" error, but depending on your code the error may be much more obscure.  Which is bad enough if you have to dig out the error, and far worse if the script is in the hands of someone who has no Bash knowledge.<br></div><div><br></div><div>I choose to use the quotes and braces as a precaution against possible future problems.  Some people don't, and that's fine.  But what I'm doing isn't "wrong" (it's valid and safe syntax), so I don't think it's worth a lot of debate.</div><div><br></div><div>-- <br></div></div><div dir="ltr" class="gmail_signature">Giles<br><a href="https://www.gilesorr.com/" target="_blank">https://www.gilesorr.com/</a><br><a href="mailto:gilesorr@gmail.com" target="_blank">gilesorr@gmail.com</a></div></div>