Burkhard
Burkhard

Reputation: 14738

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?

Upvotes: 1

Views: 252

Answers (2)

Robert Gamble
Robert Gamble

Reputation: 109012

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.

Upvotes: 7

CesarB
CesarB

Reputation: 45535

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

Upvotes: 1

Related Questions