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

Re: bash variables



In <[🔎] 20110228004108.GA3922@playground>, Mike McClain wrote:
>On Sun, Feb 27, 2011 at 12:45:27PM -0600, Boyd Stephen Smith Jr. wrote:
>> In <[🔎] 20110225222127.GA1996@playground>, Mike McClain wrote:
>> >This only bites me once in a while but when it does it can be very
>> >frustrating so any hints / tips are welcome.
>> 
>> FOO="stuff 'with' qu\"otes"
>> echo $FOO
>> echo stuff 'with' qu\"otes
>> 
>> Yes, quote removal happens after parameter expansion, but it only removes
>> quotes that existed before expansion.  So, your find and grep are choking
>> on quotes.
>
>Yes, I'm afraid you're right, they do choke on quotes,
>too many it appears.
>
>root@/deb40a:~> set -x
>root@/deb40a:~> VAR='boo "*"'; echo $VAR
>+ VAR=boo "*"
>+ echo boo '"*"'
>boo "*"
>
>root@/deb40a:~> VAR="boo '*'"; echo $VAR
>+ VAR=boo '*'
>+ echo boo ''\''*'\'''
>boo '*'
>
>Any idea why bash would put extra quotes around a quoted term
>in a variable upon expansion?

Bash is not adding quotes.[1]  It is failing to remove them.  If you really 
want to understand what is happening, install the susv2 or susv3 packages 
from contrib and read the section "Shell Command Language".  Particularly the 
expansions does by the shell at various times.

When you enter:
find -name "*"
at the bash prompt, it goes through a number of steps and then makes the C 
call:
execv("/usr/bin/find", { "find", "-name", "*" });
It has removed the quotes around the asterisk before sending it to the 
command.  The quotes prevented bash from expanding the asterisk into a list 
of (non-dot) files in the current directory.

When you enter:
FIND='find -name "*"'
at the bash prompt, it (almost) makes the C call:
setenv("FIND", "find -name \"*\"", 1);
Again it removed the (outer) quotes, which were preventing its normal 
behavior of ending the variable value at the first blank.  Then later when 
you enter:
$FIND
it goes through the same steps and makes the C call:
execv("/usr/bin/find", { "find", "-name", "\"*\"" });
It did not remove the quotes because they were not originally present in the 
input, instead the were the result of another expansion.

[1] Okay, bash is adding quotes in the trace output, but that's a visual 
debugging aid, to help advanced users count the exact number of arguments 
passed into the called command.  The called command doesn't see them.
-- 
Boyd Stephen Smith Jr.                   ,= ,-_-. =.
bss@iguanasuicide.net                   ((_/)o o(\_))
ICQ: 514984 YM/AIM: DaTwinkDaddy         `-'(. .)`-'
http://iguanasuicide.net/                    \_/

Attachment: signature.asc
Description: This is a digitally signed message part.


Reply to: