Re: bash script - quotes in variables

From: Jan C. Nordholz (jckn_at_gmx.net)
Date: 03/18/05

  • Next message: Pipo: "Re: gdm login problem"
    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
    


  • Next message: Pipo: "Re: gdm login problem"

    Relevant Pages

    • Re: understanding arrays, and their use
      ... > path enclosed by double quotes, or several space separated words, ... > field, option1) and its value (4th field, string or boolean). ... understand how to access the other fields of the array that ... > "option1" (outside the array, which is tied to a widget), and I want to ...
      (comp.lang.tcl)
    • Re: Regex help
      ... you should loop through each short string array to count each string ... > array item's quotes number, the key point is to determine if the array's ... If it has odd number of quotes, mark it as start concatenate array ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: parse bib file in C
      ... I want 2d array of the entries, say, ... Do you intend for the entire array to be in memory at once? ... conversely, all quotes occur in pairs, etc)? ... publisher = "Brooks Cole", ...
      (comp.lang.c)
    • Re: Foreach displaying data twice
      ... it should be in quotes. ... back to string, so it'll trigger some stupid notice.. ... The thing is, if you're calling a multidimensional array, you gotta use ... Thanks for setting me straight Rami! ...
      (comp.lang.php)
    • Re: Random Numbers
      ... I have a program that has character strings in an array, ... quotes to vary each time you start the program. ... method to accomplish this goal without user input? ... the conversion to integer will also happen automatically. ...
      (comp.lang.fortran)