Re: bash script - quotes in variables
From: Jan C. Nordholz (jckn_at_gmx.net)
Date: 03/18/05
- Previous message: Alvin Oga: "Re: Anyone could help with a penetration test ?"
- In reply to: Dmitry Yakovkin: "bash script - quotes in variables"
- Next in thread: Bruno Hertz: "Re: bash script - quotes in variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 18 Mar 2005 02:02:04 +0100 To: debian-user@lists.debian.org
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>
-- To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
- application/pgp-signature attachment: Digital signature
- Previous message: Alvin Oga: "Re: Anyone could help with a penetration test ?"
- In reply to: Dmitry Yakovkin: "bash script - quotes in variables"
- Next in thread: Bruno Hertz: "Re: bash script - quotes in variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|