Results 1 to 8 of 8

Thread: How to expand a variable and keep the quotation marks?

  1. #1
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    How to expand a variable and keep the quotation marks?

    Hi Guys,

    I need the output to be:
    Code:
    rainlendar2 --add "<user input> [FTASKS]"
    I can get the user input by:
    Code:
    read -p "Enter some text: " var1
    I can almost get this to work with:
    Code:
    rainlendar2 --add "$var1 [FTASKS]"
    except that gives me:
    Code:
    rainlendar2 --add user input [FTASKS]
    and I need
    Code:
    rainlendar2 --add "user input [FTASKS]"
    That is, I need quote marks before the user input and after ].
    Could someone please give me some advice on the correct way to do this?
    Thank you,
    GG -----------

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: How to expand a variable and keep the quotation marks?

    Check out: http://mywiki.wooledge.org/Quotes

    I'd try something like:
    Code:
    read -r var1
    var1="$var1 [FTASKS]"
    rainlendar2 --add "$var1"

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to expand a variable and keep the quotation marks?

    are the quotes supposed to be the part of the passed string? why would that be the case?

    Code:
    rainlendar2 --add "$var1 [FTASKS]"
    that should work if preserving the string as 1 continuous chunk of text is what you are after.

    except that gives me:

    Code:
    rainlendar2 --add user input [FTASKS]
    how exactly can you tell this is what happens, with each of these being considered a separate param?
    double quoted string undergoes param expansion but no matter what it stays in 1 piece.
    Last edited by Vaphell; November 21st, 2014 at 07:54 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: How to expand a variable and keep the quotation marks?

    Hey Vaphell,

    This is to send a todo to the caldav server.
    rainlendar2 is the desktop calendar program that syncs with the server.
    They have a gui to add tasks and events, but it is a bit cumbersome.
    I can add a task by running this in the terminal:
    Code:
     rainlendar2 --add "some task [Calendar_name]"
    Basically what I am trying to do is make it a bit easier by being able to simply enter the task descriptiion and haveing that turned into the correct format.
    So yeah that format is:
    Code:
     rainlendar2 --add "some task [FTASKS]"
    By the way, sisco311's suggestion resulted in the same as my own attempt. I ended up with:
    Code:
     rainlendar2 --add some task [FTASKS]
    According to what I see in the terminal if I add an echo command, the quotes are lost.
    Last edited by GrouchyGaijin; November 21st, 2014 at 08:31 PM. Reason: more information
    Thank you,
    GG -----------

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to expand a variable and keep the quotation marks?

    so you are adding echo in front of the command? Everything is peachy. The outermost quotes are for shell, like a parenthesis that makes sure than a continuous string is from here to there. The command itself never sees them. What matters in the end is what shell considers a param, not what you see in echo stripping the details.

    Code:
    cmd aaa bbb "ccc ddd"
     $0  $1  $2   $3
    to prove it, instead of echo try this instead printf -- '%s\n' <your stuff>

    check this out
    Code:
    $ my_cmd() { echo "params i got:"; printf -- '%s\n' "$@"; }
    $ echo my_cmd --add "some task [XXX]"
    my_cmd --add some task [XXX]            <- wtf?!?
    $ my_cmd --add "some task [XXX]"
    params i got:
    --add
    some task [XXX]       <- false alarm, everything is peachy
    Last edited by Vaphell; November 21st, 2014 at 10:29 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: How to expand a variable and keep the quotation marks?

    Vapell,

    I think I understand what you are saying, but the example you used is confusing (at least to me).
    In any case I came up with a Rube Goldberg solution:
    Code:
     
    echo rainlendar2 --add' "task' $var1' [FTASKS]"' >~/Documents/task.txt
    and the an xclip command to grab the command from the task.txt and paste it in the terminal.
    Thank you,
    GG -----------

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to expand a variable and keep the quotation marks?

    the problem is you haven't described your problem in detail so it's not entirely clear where to look. I thought it was about passing params to another program, but here i see it's about echoing some text into a file.

    Long story short:
    1. If you need to see quotes in some kind of loosely defined output, then the quotes have to be included in the content too. Shell always strips outermost quotes during parsing.
    printf is a good way to do that kind of thing because format string gets rid of a lot of hassle with escaping.
    Code:
    $ var1='abc def [WUT]'
    $ printf -v var2 'something --add "%s"' "$var1"
    $ echo "$var2"
    something --add "abc def [WUT]"


    2. If you need to make sure params are passed without mangling you wrap them in a single pair of quotes which will act as a hint for shell.

    Reading OP again i see 'i need the output to be' so i misunderstood and it's #1. In that case you need to include the quotes within the content too.
    Out of curiosity, what do you need it for?
    Last edited by Vaphell; November 22nd, 2014 at 06:49 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: How to expand a variable and keep the quotation marks?

    Thanks man,

    Basically this is just so I can quickly submit todo items and events to a calendar on a remote server.
    There is a gui to the desktop calendar program I am using, but it is cumbersome - the cli is quicker. I'm lazy though and only wanted to have to type the message and not the rest of the command.
    Thank you,
    GG -----------

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •