Table of Contents
Newsgroups: comp.unix.shell Date: Mon, 30 Oct 2006 07:47:22 -0500
> 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. ... For that matter, when would
> the assignment to LOCKDIR have to be quoted? If there is a whitespace?
>
> 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/lockWithout 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