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

Re: bash script - quotes in variables



On Fri, Mar 18, 2005 at 03:42:28AM +0300, Dmitry Yakovkin wrote:
> Hi everybody,
> 
> I'm about to bang my head on the nearest wall, would you please point
> me to my mistake.
> 
> Problem: I need to send scripted email with additional headers (like:
> mail -a "Mime-version: 1.0" recipient@somewhere.org)
> 
> Okay, it works beautifully from an interactive prompt:
>   mail -a "Mime-version: 1.0" -a approved:listpass -s "Test subject"
> mylist@mysite.com
> 
> now I want to do that from inside a shell script:
> 

Hi Dmitry.

Once the quotes are stuffed into the variable, they lose any special
meaning to the shell, especially the effect of preventing word splitting.
So once you set a variable to

VAR="bla 'blip blop' bla morebla"

there will be five words, regardless of any occurrences of ' or "
therein.

I'd suggest using an array. Assign your options this way:

MAIL_OPTS=(-a "Mime-version: 1.0" -a Approved:mypassword)

This constructs MAIL_OPTS as an array, containing four words (here
the quote prevents word splitting):

${MAIL_OPTS[0]}   -a
${MAIL_OPTS[1]}   Mime-version: 1.0
${MAIL_OPTS[2]}   -a
${MAIL_OPTS[3]}   Approved:mypassword

Later you have to expand the array with

[...] "${MAIL_OPTS[@]}" [...]

This expands the whole array, and esp. _each array member to its
own word_ - no more and no less. Exactly what you want. Take care
that neither the two different types of brackets nor the double
quotes around the expansion are optional - it's all required. See
the bash(1) manpage, Section "Arrays". :)


Regards,

Jan

-- 
Jan C. Nordholz
<jckn At gmx net>

Attachment: signature.asc
Description: Digital signature


Reply to: