help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Recursive expansion


From: Bill Gradwohl
Subject: [Help-bash] Recursive expansion
Date: Fri, 10 Feb 2012 15:13:24 -0600

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
+ touch '*'
+ simple
+ local file
+ for file in '*'
+ echo file= '*' test len=1
file= * test len=1
+ echo 'file=*' len=1
file=* len=1
+ : file= '*' test len=1
+ : 'file=*' len=1
+ for file in '*'
+ echo file= test len=4
file= test len=4
+ echo file=test len=4
file=test len=4
+ : file= test len=4
+ : file=test len=4

+ echo file= '*' test len=1
file= * test len=1
+ echo 'file=*' len=1
file=* len=1
+ : file= '*' test len=1
+ : 'file=*' len=1
+ for file in '*'
+ echo file= test len=4
file= test len=4
+ echo file=test len=4
file=test len=4
+ : file= test len=4
+ : file=test len=4

During execution, there are 2 files referenced - '*' and 'test'. The file named '*' is the one giving me grief.

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 appears that * is getting expanded again - recursively, and I can't see why that's happening.


================================
#!/bin/bash
set -x
simple() {
   local file

   for file in *; do
      echo file= $file len=${#file}
      echo file=$file len=${#file}
      : file= $file len=${#file}
      : file=$file len=${#file}
   done
}

touch '*'
simple
===============================

--
Bill Gradwohl


reply via email to

[Prev in Thread] Current Thread [Next in Thread]