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:
alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234
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.
alias splitpath='echo $PATH | awk -F : '"'"'{print "PATH is set to"} {for (i=1;i<=NF;i++) {print "["i"]",$i}}'"'"
It works when there are both single quotes and double quotes in the alias string!My interpretation: bash implicitly concatenates differently quoted string expressions.
worked for me, example of double escaped single quotes:
alias serve_this_dir='ruby -rrack -e "include Rack;Handler::Thin.run Builder.new{run Directory.new'"'"''"'"'}"'
Certainly not the most readable solution. It over-uses single quotes where they are not really needed.
I contend that
'\''
is vastly more readable in most contexts than'"'"'
. In fact, the former is almost always clearly distinct within a single-quoted string, and thus is just a matter of mapping it semantically to the "it's an escaped quote" meaning, as one does with\"
in double-quoted strings. Whereas the latter blends into one line of quote ticks and needs careful inspection in many cases to properly distinguish.