views:

150

answers:

2

I have a simple linux script:

#!/bin/sh
for i in `ls $1`
do
       echo $i
done

In my temp folder are 4 file: a.a, a.aa, a.ab and a.ac

When i call ./script temp/*.?? i get:

temp/a.aa

When i call ./script "temp/*.??" i get:

temp/a.aa
temp/a.ab
temp/a.ac

Why do the double quote change the result?

+1  A: 

Because without the quotes the shell is expanding your call to:

./script temp/a.aa temp/a.ab temp/a.ac

So $1 is temp/a.aa instead of temp/*.??.

CesarB
+7  A: 

In the first case the shell expands temp/*.?? to:

temp/a.aa temp/a.ab temp/a.ac

Since you are only looking at the first parameter in your script only temp/a.aa is passed to ls.

In the second case, the shell does not perform any expansion because of the quotes and the script receives the single argument temp/*.?? which is expanded in the call to ls.

Robert Gamble