[ale] Undesired shell expansion building a command line

JK jknapka at kneuro.net
Wed Aug 26 15:47:51 EDT 2009


Michael B. Trausch wrote:
> I have the following in a shell script:
> 
> EXCLUDED="/dev /sys /proc /tmp /bin /usr/bin /usr/sbin /sbin"
> EXCLUDED="${EXCLUDED} /lib /usr/lib *~"
> 
> EXCLUDE_ARGS=""
> for ARG in $EXCLUDED; do
>     EXCLUDE_ARGS="${EXCLUDE_ARGS} --exclude ${ARG}"
> done
> 
> The end result that I expect is to build the following:
> 
> --exclude /dev --exclude /sys --exclude /proc --exclude /tmp
> --exclude /bin --exclude /usr/bin --exclude /usr/sbin --exclude /sbin
> --exclude /lib --exclude /usr/lib --exclude *~
> 
> But, when each item in $EXCLUDED is evaluated to become $ARG, it appears
> to undergo filename expansion.  If no files fitting the pattern '*~'
> exist in the current directory, it evaluates fine.  On the other hand,
> if there are files matching '*~', it expands to those filenames.  I'd
> like to suppress globbing while building that so that it works always,
> but I actually don't know where to look on how.  It seems that there
> isn't a way around the expansion while evaluating the loop.
> 
> I hope I'm wrong.  Am I?

Try:

   # Turn pathname expansion off (temporarily).
   set -f
   EXCLUDED="${EXCLUDED} /lib /usr/lib *~"
   # And assuming you want to turn pathname expansion back on:
   set +f

-- JK



More information about the Ale mailing list