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. ...
- 06-08-2009 #1Just 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:
I can make this work in simple script as follows:Code: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 cvlc "source" --sout '#duplicate'
This gives the following output:Code:#!/bin/bash CMD="cvlc \"source\" --sout '#duplicate'" echo Command is: $CMD $CMD
...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.Code:Command is: cvlc "source" --sout '#duplicate'
Can anyone see what I might have done wrong or suggest a correction?
Thanks!
- 06-08-2009 #2Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
Not sure but try this to execute the command:
Code:eval $CMD
- 06-09-2009 #3Just 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:' ' Everything between ' and ' is taken literally except for another '.
$ Variable substitution will occur.`Command substitution will occur" This marks the end of the double quote.
\ 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:
I hope these tips help others some day.Code:#!/bin/bash -x


Reply With Quote