bash + sed: quadruple backslash for proper escaping within an alias
Posted by jpluimers on 2020/06/09
This is part of a bash alias where I had to use quadruple backslash in order to escape it for sed:
# The sed with quad //// is to prevent 'unterminated substitute in regular expression':
alias x='... | sed "s/=.*/ \\\\/"'
This is needed because bash will escape \\\\
into \\
which sed then escapes to \
.
The easiest way to find this is to replace the sed
with echo
to see the expansion.
References:
- explanation at [WayBack] regex – Why does sed require 3 backslashes for a regular backslash? – Stack Overflow (by Dennis Williamson).
- echo tip at [WayBack] regex – Why does sed require 3 backslashes for a regular backslash? – Stack Overflow (byRoger Pate who left StackOverflow).
–jeroen
Mike Verhagen said
“This is needed because bash will escape \\ into \ which sed then escapes to .”
this makes me smile….when you know you have just one level of indirection, it’s s genius move.
jpluimers said
After swearing, then figuring out, I had to smile too (: