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_quotesJC'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 techniquealias esc_single_quotes=$'sed \s|'|\\\\'|g\'# 2) My second techniquealias esc_single_quotes=sed 's|'\''|\\\'\''|g'# 3) My third techniquealias 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-topalias esc_single_quotes='sed '''s|'|\\\\'|g''# 5) Technique inspired from http://stackoverflow.com/posts/1315213/revisionsalias esc_single_quotes='sed '\''s|'|\\\\'|g'\'''Would anyone live up to this impossible challenge & prove me wrong?
Escaping single quotes **with** an alias
bash;sed;alias
I can escape everything at once with printf %q\n $stringBut here is your answer :alias esc_single_quotes=sed \s/'/\\\\\\\\'/g\echo  $string | esc_single_quotes JC\'s alias to escape double quotesor :alias esc_single_quotes='sed s/'\''/\\\\'\''/g'echo  $string | esc_single_quotes JC\'s alias to escape double quotes