Terrence Brannon
Terrence Brannon

Reputation: 4968

why is my loop variable not alive after 1 iteration?

I have this code

for p in "abra/cadabra reach/out"
do
        r="$HOME/x/$p"
        echo $r
done

But it only produces this:

/Users/terrencemonroebrannon/x/abra/cadabra reach/out

Not

/Users/terrencemonroebrannon/x/abra/cadabra /Users/terrencemonroebrannon/x/reach/out

as I would expect.

Upvotes: 1

Views: 77

Answers (2)

larsks
larsks

Reputation: 311516

You have placed your two items in quotes, which bash interprets as a single token. That's what quotes are for. You would get the behavior you describe if you were to remove the quotes:

for p in abra/cadabra reach/out
do
        r="$HOME/x/$p"
        echo $r
done

Gives me:

/Users/lars/x/abra/cadabra
/Users/lars/x/reach/out

Upvotes: 5

kev
kev

Reputation: 161654

bash will see "abra/cadabra reach/out" as one token.
Quotes are not needed.

for p in abra/cadabra reach/out
do
        r="$HOME/x/$p"
        echo $r
done

Upvotes: 2

Related Questions