Re: Question re: braces and quotes



In article <Z6m1h.6506$Wy6.4104@trnddc01>,
"Peter" <peter@xxxxxxxxxxxxx> wrote:

I was reviewing the Suse/Gentoo run-crons script, and comparing it with
some elementary scripts I had written. All of a sudden, I realized that
despite reading the docs and advanced scripting manual, I really did not
have a good grasp on the concept of when to quote, enclose in a brace, etc.

This snippet crystallizes my confusion. As written, the variable
definition for LOCKFILE uses braces, but I found that it made no
difference whether or not I used braces, quotes, or any combination. I
understand that certain test contructs, such as [ -n... ] require quotes,
and if I want to manipulate a variable, braces are useful for
substitution, but could someone please explain if the first assignment of
LOCKFILE is proper, and why? For that matter, when would the assignment to
LOCKDIR have to be quoted? If there is a whitespace?

Thanks a lot!

#!/bin/sh

LOCKDIR=/var/spool/cron/lastrun
LOCKFILE=${LOCKDIR}/lock

echo "braces no quotes $LOCKFILE"

LOCKFILE="${LOCKDIR}"/lock
echo "braces quote $LOCKFILE"

LOCKFILE=$LOCKDIR/lock
echo "no braces no quote $LOCKFILE"

LOCKFILE="$LOCKDIR"/lock
echo "no braces quote $LOCKFILE"

None of the above examples need the braces or quotes. But here are some
examples where they're necessary:

LOCKFILE=${LOCKDIR}1/lock

Without the braces, it would look for a variable named LOCKDIR1.

LOCKFILE="$LOCKDIR/lock file with spaces"

Without the quotes, it would set the environment variable to
$LOCKDIR/lock while trying to execute the command line "file with
spaces".

STRINGWITHSPACES="foo bar"
echo $STRINGWITHSPACES
echo "$STRINGWITHSPACES"

The first one loses the multiple spaces between the words.

However, you don't need it here:

OTHERSTRING=$STRINGWITHSPACES

because word splitting of the assignment portion of a command is done
before variable expansion.

--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.



Relevant Pages

  • Re: Question re: braces and quotes
    ... echo "braces no quotes $LOCKFILE" ... None of the above examples need the braces or quotes. ...
    (comp.unix.shell)
  • Re: get current date
    ... code using barewords here, "quotes" there and elsewhere. ... put aside the obvious substitution-issue, ... Using {braces} sometimes allows the bytecode compiler to do a better ...
    (comp.lang.tcl)
  • Question re: braces and quotes
    ... I was reviewing the Suse/Gentoo run-crons script, ... have a good grasp on the concept of when to quote, enclose in a brace, etc. ... difference whether or not I used braces, quotes, or any combination. ... LOCKFILE is proper, and why? ...
    (comp.unix.shell)
  • Re: What is wrong with: puts "Data={" ?
    ... >>outside of any surrounding blocks, ... if puts "" took the inner braces into account when doing the outer ... echo "$USER" ...
    (comp.lang.tcl)
  • Re: Problem with exec command
    ... | I am pretty much a novice, but had a similar problem recently. ... | $webaddress wouldn't substitute in braces ... ... and in addition he said to use 'list' instead of quotes to ... up the webaddress in question (yes, ...
    (comp.lang.tcl)