On 8/12/19, Greg Wooledge <wool...@eeg.ccf.org> wrote: > On Mon, Aug 12, 2019 at 01:56:46PM -0400, Lee wrote: >> What's the difference between ${d} and "${d}"? Or is that a bashism >> also? (all my scripts use /bin/sh so I'm pretty clueless wrt bash) > > This applies to both sh and bash. > > An unquoted substitution, like $d or ${d}, undergoes several steps. The > first step is actually copying the contents of the variable. After that > comes word splitting (dividing the content into words/fields using IFS), > and then pathname expansion ("globbing"). > > I have a helper script called "args" which I use to illustrate this stuff. > > wooledg:~$ cat bin/args > #!/bin/sh > printf "%d args:" "$#" > printf " <%s>" "$@" > echo > > Using that, we can demonstrate: > > wooledg:~$ d="a variable" > wooledg:~$ args "$d" > 1 args: <a variable> > wooledg:~$ args $d > 2 args: <a> <variable> > > The curly braces don't matter in this case, because there's nothing after > the $d for it to matter. > > wooledg:~$ args ${d} > 2 args: <a> <variable> > > The curly braces are only needed because of the _stuff after the d. > Without them, d_stuff is treated as a variable name. > > wooledg:~$ args "$d_stuff" > 1 args: <> > wooledg:~$ args "${d}_stuff" > 1 args: <a variable_stuff> > > The quotes are still needed. Without them, we still get word splitting > and pathname expansion. > > wooledg:~$ args ${d}_stuff > 2 args: <a> <variable_stuff> > > > For more details, see <https://mywiki.wooledge.org/Quotes>.
Wow! It's going to take me a while to understand the implications of all that, so I'll just stop here with a Thank you so much for the detailed explanation! Lee