Saturday, February 5, 2011

Why do double-quote change the result

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?

  • 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/*.??.

    From CesarB
  • 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.

0 comments:

Post a Comment