Single quotes within a double-quoted string on the Linux command line… here is a solution to the problem.
Single quotes within a double-quoted string on the Linux command line
I have the following command
cmd '"asdf" "a'sdf"'
I just need to enclose the parameters in single quotes. For some reason I don’t know, cmd can’t use double quotes. The above command does not work because the middle single quote terminates the first single quote. If I run away, go below
cmd '"asdf" "a\'sdf"'
Still doesn’t work. How do I get it to work?
Solution
According to the bash man page:
Enclosing characters in single quotes preserves the literal value
of each character within the quotes. A single quote may not occur
between single quotes, even when preceded by a backslash.
So the answer is, no matter how you try, you can’t include single quotes in a single quote string. But depending on how the script is set up, you can use ‘\''
, which will end the first single quote, escape the second, and start another single quote string with the third.