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?