Let's say, you have a bash alias
like:
alias rxvt='urxvt'
which works fine.
However:
alias rxvt='urxvt -fg '#111111' -bg '#111111''
won't work, and neither will:
alias rxvt='urxvt -fg \'#111111\' -bg \'#111111\''
So how do you end up matching up opening and closing quotes inside a string once you have escaped quotes?
alias rxvt='urxvt -fg'\''#111111'\'' -bg '\''#111111'\''
seems ungainly although it would represent the same string if you're allowed to concatenate them like that.
If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:
Explanation of how
'"'"'
is interpreted as just'
:'
End first quotation which uses single quotes."
Start second quotation, using double-quotes.'
Quoted character."
End second quotation, using double-quotes.'
Start third quotation, using single quotes.If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.
IMHO the real answer is that you can't escape single-quotes within single-quoted strings.
Its impossible.
If we presume we are using bash.
From bash manual...
You need to use one of the other string escape mechanisms " or \
There is nothing magic about
alias
that demands it use single quotes.Both the following work in bash.
The latter is using \ to escape the space character.
There is also nothing magic about #111111 that requires single quotes.
The following options achieves the same result the other two options, in that the rxvt alias works as expected.
You can also escape the troublesome # directly
Obviously, it would be easier simply to surround with double quotes, but where's the challenge in that? Here is the answer using only single quotes. I'm using a variable instead of
alias
so that's it's easier to print for proof, but it's the same as usingalias
.Explanation
The key is that you can close the single quote and re-open it as many times as you want. For example
foo='a''b'
is the same asfoo='ab'
. So you can close the single quote, throw in a literal single quote\'
, then reopen the next single quote.Breakdown diagram
This diagram makes it clear by using brackets to show where the single quotes are opened and closed. Quotes are not "nested" like parentheses can be. You can also pay attention to the color highlighting, which is correctly applied. The quoted strings are maroon, whereas the
\'
is black.(This is essentially the same answer as Adrian's, but I feel this explains it better. Also his answer has 2 superfluous single quotes at the end.)
I'm not specifically addressing the quoting issue because, well, sometimes, it's just reasonable to consider an alternative approach.
which you can then call as:
the idea being that you can now alias this without concern for quotes:
or, if you need to include the
#
in all calls for some reason:which you can then call as:
then, of course, an alias is:
(oops, i guess i kind of did address the quoting :)
Another way to fix the problem of too many layers of nested quotation:
You are trying to cram too much into too tiny a space, so use a bash function.
The problem is you are trying to have too many levels of nesting, and the basic alias technology is not powerful enough to accommodate. Use a bash function like this to make it so the single, double quotes back ticks and passed in parameters are all handled normally as we would expect:
Then you can use your $1 and $2 variables and single, double quotes and back ticks without worrying about the alias function wrecking their integrity.
This program prints:
Here is another solution. This function will take a single argument and appropriately quote it using the single-quote character, just as the voted answer above explains:
So, you can use it this way: