Re: Question re: braces and quotes
- From: "Peter" <peter@xxxxxxxxxxxxx>
- Date: Mon, 30 Oct 2006 13:18:15 GMT
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"Again, would braces be superfluous here?
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.
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
.
- Follow-Ups:
- Re: Question re: braces and quotes
- From: Bill Marcum
- Re: Question re: braces and quotes
- References:
- Question re: braces and quotes
- From: Peter
- Re: Question re: braces and quotes
- From: Barry Margolin
- Question re: braces and quotes
- Prev by Date: Re: Question re: braces and quotes
- Next by Date: Re: question about setenv
- Previous by thread: Re: Question re: braces and quotes
- Next by thread: Re: Question re: braces and quotes
- Index(es):
Relevant Pages
|