Bash: Escaping strings easily
Enter a line of Bash starting with a
#comment, then run!:qon the next line to see what that would be with proper Bash escaping applied.
bash-3.2$ # This string 'has single' "and double" quotes and a $
bash-3.2$ !:q
'# This string '\''has single'\'' "and double" quotes and a $'
bash: # This string 'has single' "and double" quotes and a $: command not found
The
!character begins a history expansion;!stringproduces the last command beginning withstring, and:qis a modifier that quotes the result; so I’m guessing this is equivalent to!stringwherestringis"", so it produces the most recent command, just like!!does.
Leave a comment