[NCLUG] /etc/environment

Bob Proulx bob at proulx.com
Sun Aug 19 19:47:32 MDT 2007


Alan Silverstein wrote:
> And other shell expansions, using $(...), rather than ``, which are
> harder to read (in my opinion), and don't nest.  The addition of $() was
> a great leap forward.  Example:

Agreed.

>     foo="$(echo 'abc' $(wc -l bar))"
> 
> Dunno if the "" are necessary, but they don't hurt, might be needed in
> some cases, and make it easier to read (again, in my opinion).

The quotes here are not needed but won't hurt.  I would rather see
them and not need them than need them and not have them.  But like
parens I prefer not to have too many of them to the point that it
makes it hard to read.  But quotes are like parens, a matter of taste
and style.

  var="$(echo "$(date -R)": "$(wc -l afile)")"

Versus:

  var=$( echo $(date -R): $(wc -l afile) )

One way to remember if quotes are needed is if the operation is
completely _internal to the shell_ then quotes are not needed.

  var1=FOO
  var2=$var1
  var3=$(echo something)

  case $variable in

  if [[ $variable < $othervar ]]

None of those need quotes because they are completely internal to the
shell.  But if it is external to the shell then quotes are needed.  Or
if it is now internal but was originally external (such as test) and
therefore must behave the same then quotes and other protection is needed.

  if [ X"$variable" = Xsomething ]

  if test -f "$variable"

  var1="FOO  BAR"
  var2=$(echo "$var1")
  echo "$var2"

  expr "$var1" : FOO

HTH,
Bob



More information about the NCLUG mailing list