[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Recursive expansion
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Recursive expansion |
Date: |
Fri, 10 Feb 2012 16:34:15 -0500 |
User-agent: |
Mutt/1.4.2.3i |
On Fri, Feb 10, 2012 at 03:13:24PM -0600, Bill Gradwohl wrote:
> If I take the script below, drop it into an empty directory, and execute
> it, I get:
>
> address@hidden junk# ./test
> + touch '*'
> + simple
> + local file
> + for file in '*'address@hidden junk# ./test
> It appears that * is getting expanded again - recursively, and I can't see
> why that's happening.
It looks like you ran this thing twice, at the same time. I'm not seeing
any recursion. I'm just seeing two overlapping outputs.
> + touch '*'
> + simple
> + local file
> + for file in '*'
> + echo file= '*' test len=1
> file= * test len=1
> + echo 'file=*' len=1
> file=* len=1
(snip)
> Can someone explain why when $file contains the asterisk character it gets
> expanded to the directory contents if and only if $file is surrounded by
> white space. Note the reported length of the contents of $file. Even when
> the contents clearly has a length of 1, '* test' gets substituted.
It's the lack of quoting.
$ file=*
$ echo "$file"
*
$ echo $file
0017 2 2009-06-03.txt a abnormal admin adu anonymized-2011-03-23.hl7 ....
An unquoted parameter expansion undergoes word splitting, and then
globbing. That's why you always, always, always quote your expansions.
for file in *; do
echo "File <$file> name has length ${#file}"
done
Without the double quotes, it all goes to hell. If you're lucky, it
goes to hell right away, and you notice it and fix it. More often, it
goes to hell at 3 AM on a Saturday.