1

I am aware that there are many questions asked in the past about escaping single quotes, but none of them addresses this challenge, i.e how to do it with an alias.

I have already successfully defined many aliases to replace sed/awk/grep/perl commands which use regex in the last release of bash (4.4.0(1)).

For instance, the following alias can escape double quotes in any string:

alias esc_double_quotes=$'sed \'s|"|\\\\"|g\''
string="JC's alias to escape \"double quotes\""
echo "$string" | esc_double_quotes
JC's alias to escape \"double quotes\"

However, when it comes to escaping single quotes with an alias, it seems that the mission is impossible.

I have already tried 5 different methods which all fail for different reasons:

# 1) My first technique
alias esc_single_quotes=$'sed \"s|'|\\\\'|g\"'
# 2) My second technique
alias esc_single_quotes="sed 's|'\''|\\\'\''|g'"
# 3) My third technique
alias esc_single_quotes="sed \"s|'|\\\\'|g\""
# 4) Technique inspired from http://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings?answertab=active#tab-top
alias esc_single_quotes='sed '"'"'s|'|\\\\'|g'"'"
# 5) Technique inspired from http://stackoverflow.com/posts/1315213/revisions
alias esc_single_quotes='sed '\''s|'|\\\\'|g'\'''

Would anyone live up to this impossible challenge & prove me wrong?

3 Answers3

2

I can escape everything at once with

printf "%q\n" "$string"

But here is your answer :

alias esc_single_quotes="sed \"s/'/\\\\\\\\'/g\""
echo  "$string" | esc_single_quotes 
JC\'s alias to escape "double quotes"

or :

alias esc_single_quotes='sed "s/'\''/\\\\'\''/g"'
echo  "$string" | esc_single_quotes 
JC\'s alias to escape "double quotes"
Vouze
  • 839
  • Bravo and thanks :-) I won't ask why so many backslashes are needed though. However, the first part of your answer does not answer the question because it escapes also spaces and double quotes. – jean-christophe Manciot Feb 14 '17 at 17:21
  • You need so many backslashes, because the string will be interpreted twice by the shell and once by sed. So you need 2x2x2=8 backslashes in order to have one at the end. Type "alias" alone to see the first level of interpretation. – Vouze Feb 16 '17 at 09:14
1

To help our eyes, let's simplified it:

$ alias esc_single_quotes='sed "s|\x27|\x5c\x5c\x27|g"'

$ echo "this is 'something'"
this is 'something'

$ echo "this is 'something'" |esc_single_quotes
this is \'something\'
0
#!/usr/bin/env bash

function esc_double_quotes() {
    echo $* | sed 's|"|\\\\"|g'
}
  • @don_crissti, Don't leave me in suspense here, what did I miss? I tried to show the right way to define the alias that he needed. – Stephen Rauch Feb 13 '17 at 15:00
  • 1
    @don_crissti, Oh I got that part, but from the help center - Read the question carefully. What, specifically, is the question asking for? Make sure your answer provides that – or a viable alternative. - So I thought it was viable. – Stephen Rauch Feb 13 '17 at 15:08
  • 1
    The question talks about how to escape SINLGE quotes, no? – George Vasiliou Feb 13 '17 at 15:48