Re: Question re: braces and quotes



On Mon, 30 Oct 2006 07:47:22 -0500, Barry Margolin wrote:

snip...
#!/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.

Don't I feel like the dunce! Why do you think it WAS coded that way? Are
braces always "safe" to use -- that is, will bash always properly handle
it as a variable and expand it properly? What about quoted braces and
variables? When would they be required?

But here are some examples where they're necessary:

LOCKFILE=${LOCKDIR}1/lock

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


Yes, I experienced that! Took a while to figure it out! So, the braces
encapsulate the variable.

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".

Again, would braces be superfluous here?

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

The first one loses the multiple spaces between the words.


Tricky. Must be hell to debug something like that!

However, you don't need it here:

OTHERSTRING=$STRINGWITHSPACES


I love consistency! What if you DID use quotes? Same result?

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


thanks for the explanation!

Is there an easy way to generalize when braces are used? I have seen
assignments in the same script that inconsistently use it. Very hard for
an end user to understand!

For example, look at this line from run-crons:

# base is one of hourly, daily, etc. single word, no spaces
# lockdir has slashes but no spaces

if [ -e ${LOCKDIR}/cron.$BASE ]

Is BASE not quoted or braced because it is a single word with no escape
type chars? Why is LOCKDIR in braces? Why are quoted UNnecessary?

--
Peter
.



Relevant Pages

  • Re: Question re: braces and quotes
    ... difference whether or not I used braces, quotes, or any combination. ... LOCKFILE is proper, and why? ... echo "braces no quotes $LOCKFILE" ...
    (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)
  • 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)
  • Re: Datagrid Template column is neither a DataColumn nor a DataRel
    ... The problem seems to be with the fact that the MDX Query returns the data ... with column names that include braces and dots. ... if i were able to wrap single quotes around the ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • 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)