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.
I just use shell codes.. e.g.
\x27
or\\x22
as applicable. No hassle, ever really.In the given example, simply used double quotes instead of single quotes as outer escape mechanism:
This approach is suited for many cases where you just want to pass a fixed string to a command: Just check how the shell will interpret the double-quoted string through an
echo
, and escape characters with backslash if necessary.In the example, you'd see that double quotes are sufficient to protect the string:
Since Bash 2.04 syntax
$'string'
(instead of just'string'
; warning: do not confuse with$('string')
) is another quoting mechanism which allows ANSI C-like escape sequences and do expansion to single-quoted version.Simple example:
In your case:
Common escaping sequences works as expected:
Below is copy+pasted related documentation from
man bash
(version 4.4):Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
The expanded result is single-quoted, as if the dollar sign had not been present.
See Quotes and escaping: ANSI C like strings on bash-hackers.org wiki for more details. Also note that "Bash Changes" file (overview here) mentions a lot for changes and bug fixes related to the
$'string'
quoting mechanism.According to unix.stackexchange.com How to use a special character as a normal one? it should work (with some variations) in bash, zsh, mksh, ksh93 and FreeBSD and busybox sh.
I don't see the entry on his blog (link pls?) but according to the gnu reference manual:
so bash won't understand:
alias x='y \'z '
however, you can do this if you surround with double quotes:
This function:
allows quoting of
'
inside'
. Use as this:If the line to quote gets more complex, like double quotes mixed with single quotes, it may become quite tricky to get the string to quote inside a variable. When such cases show up, write the exact line that you need to quote inside an script (similar to this).
Will output:
All correctly quoted strings inside single quotes.
I always just replace each embedded single quote with the sequence:
'\''
(that is: quote backslash quote quote) which closes the string, appends an escaped single quote and reopens the string.I often whip up a "quotify" function in my Perl scripts to do this for me. The steps would be:
This pretty much takes care of all cases.
Life gets more fun when you introduce
eval
into your shell-scripts. You essentially have to re-quotify everything again!For example, create a Perl script called quotify containing the above statements:
then use it to generate a correctly-quoted string:
result:
which can then be copy/pasted into the alias command:
(If you need to insert the command into an eval, run the quotify again:
result:
which can be copy/pasted into an eval: