[SLL] Simple Bash execution of string: How do I execute the string?

Derek Simkowiak dereks at realloc.net
Tue Jun 10 16:35:32 PDT 2008


Xeno>/ Perhaps curl is doing something special, but I don't think so./

    It's a shell parsing issue, not related to curl.  The double-quotes 
from your assignment to $x are causing the single-quotes to be 
interpreted as literal argument values, instead of a shell parsing 
construct.

    To illustrate the root cause, you can get the exact same error if 
you replace the single quotes in your curl command with 
backslash-escaped double-quotes:

dereks at dell-laptop:~/temp$ y="curl -L -i -H \"Content-Type: text/html; 
charset=utf-8\" www.cool-st.com"
dereks at dell-laptop:~/temp$ echo $y
curl -L -i -H "Content-Type: text/html; charset=utf-8" www.cool-st.com
dereks at dell-laptop:~/temp$ new_var=`$y`
[snip]
curl: (6) Couldn't resolve host 'charset=utf-8"'

    The escaped \" above is treated as a meaningless literal ASCII 
character, which is not actually quoting an argument on the command 
line.  It has no meaning to the shell.  The same thing is happening to 
your single-quotes around the Content-Type header, because they are 
wrapped in the double-quotes of the var assignment to $x.

    You can fix this by using "eval", which will re-evaluate the 
expression for special shell chars.  It will work for your version (with 
single quotes) and also my version (with escaped literal double-quotes):

dereks at dell-laptop:~/temp$ new_var=`eval $x`
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  
Current
                                 Dload  Upload   Total   Spent    Left  
Speed
100  2814  100  2814    0     0   336k      0 --:--:-- --:--:-- --:--:-- 
1656k
dereks at dell-laptop:~/temp$ new_var=`eval $y`
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  
Current
                                 Dload  Upload   Total   Spent    Left  
Speed
100  2814  100  2814    0     0   345k      0 --:--:-- --:--:-- --:--:-- 
1656k
dereks at dell-laptop:~/temp$

    Finally, as a purely stylistic issue, note that some people consider 
the alternative syntax $() easier to read than the backtick characters:

dereks at dell-laptop:~/temp$ new_var=$(eval $x)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  
Current
                                 Dload  Upload   Total   Spent    Left  
Speed
100  2814  100  2814    0     0   341k      0 --:--:-- --:--:-- --:--:-- 
1656k
dereks at dell-laptop:~/temp$

--Derek

Xeno Campanoli wrote:
> I've got a simple call:
>
> curl -L -i -H 'Content-Type: text/html; charset=utf-8' host
>
> which works great, but when I go:
>
> x="curl -L -i -H 'Content-Type: text/html; charset=utf-8' host"
>
> (and I can check it, and it looks the same as the one that works when 
> I go echo $x) then doing:
>
> `$x` is not working for me.  It is getting the parts of the header 
> string as separate arguments:
>
> curl: (6) Couldn't resolve host 'text'
>
> curl: (6) Couldn't resolve host 'charset=utf-8''
>
> This is a little nutty, but I've been shell programming for years, and 
> I'm not seeing what's wrong here.  Perhaps curl is doing something 
> special, but I don't think so.  I guess I've just not done this 
> before.  Anybody have a clue?
>
> Thanks in advance.



More information about the linux-list mailing list