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?
echowhich in zsh treats backslashes specially. Tryecho 'foo\nbar'in Bash and in zsh, and then do the same withecho -eandecho -E. – ilkkachu Jun 29 '22 at 07:18xpg_echooption in bash to make itsechoUNIX compliant in that regard, and thebsdechooption inzshto 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