Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I'm relatively new to Linux so forgive me if the answer to this question is simple. I'm attempting to use a script to invoke VLC from the command line. ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20

    Single-quotes in variables

    Hello,

    I'm relatively new to Linux so forgive me if the answer to this question is simple.

    I'm attempting to use a script to invoke VLC from the command line. Some of the VLC parameters are enclosed in single quotes and some of the parameters are enclosed in double-quotes. The command typically looks like this:

    Code:
    cvlc "source" --sout '#duplicate'
    I can make this work in simple script as follows:

    Code:
    #!/bin/bash
    cvlc "source" --sout '#duplicate'
    What I would like to do is to assign the entire command to a variable. I tried the following by escaping the double-quotes:

    Code:
    #!/bin/bash
    CMD="cvlc \"source\" --sout '#duplicate'"
    echo Command is: $CMD
    $CMD
    This gives the following output:

    Code:
    Command is: cvlc "source" --sout '#duplicate'
    ...so I believe the $CMD variable is correctly formatted. Yet VLC rejects the parameters when I use the CMD variable. I'm sure this is something to do with the way bash handles single-quotes and double-quotes rather than any kind of error in VLC.

    Can anyone see what I might have done wrong or suggest a correction?

    Thanks!

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Not sure but try this to execute the command:

    Code:
    eval $CMD

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    20
    Thanks for the suggestion. I've found a solution that works for me now. From experimentation it seems that single quotes and double quotes are often (always?) interchangable so, by changing the single quotes to double quotes, I can make my script behave correctly.

    I found the following text in an a dusty textbook:

    The characters below can be used for quoting:
    " " Everything between " and " is taken literally, except for the following characters that keep their special meaning:
    $ Variable substitution will occur.
    `Command substitution will occur
    " This marks the end of the double quote.
    ' ' Everything between ' and ' is taken literally except for another '.
    \ The character following \ is taken literally. Use within "" to escape ", $ and `. Often used to escape itself, spaces and newlines.


    I also found that adding "-x" to #!/bin/bash is helpful for tracing script execution:

    Code:
    #!/bin/bash -x
    I hope these tips help others some day.

Posting Permissions

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