I have a string S
, for which I want to replace every b
character with \\
(two forward slashes).
So this is what I have:
$ S="abc"
$ A=$(echo $S | sed 's/b/\\\\/')
In bash I get the expected output:
$ echo $A
a\\c
but in zsh I have
$ echo $A
a\c
It seems to me that zsh's behavior is unexpected, why does it behave this way?
I want A
to contain two forward slashes (so I can use it later as a regex, for example). How can I achieve this in a cross-shell way?
echo
which in zsh treats backslashes specially. Tryecho 'foo\nbar'
in Bash and in zsh, and then do the same withecho -e
andecho -E
. – ilkkachu Jun 29 '22 at 07:18xpg_echo
option in bash to make itsecho
UNIX compliant in that regard, and thebsdecho
option inzsh
to make it non-compliant. – Stéphane Chazelas Jun 29 '22 at 07:24A="${S/b/\\\\}"
instead of calling out tosed
, see e.g. http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion – ilkkachu Jun 29 '22 at 07:26