sh differs from
bash and behaves different.
I guess that
sh is really
BTW: there is different behavior with this command if you check different shells.
zsh as well as
ksh on Solaris print one backslash
ksh on Linux print two backslashes.
From looking at the $shell -x output, I believe that one backslash is the correct output.
I have no idea why
ksh behaves this way on Linux. It could be that it tries to mimic
bash the behavior can be explained: bash has a non-POSIX
echo that does not interpret backshlashes as required by POSIX.
POSIX permits the behavior of
bash only on small embedded systems and POSIX otherwise requires the so called
XSI extensions to be implemented. With
echo, this requires to fully follow the behavior of
echo as impleented in 1982 by AT&T for SYSv.
An important note:
If you have a script that starts with
and that is executable (
x bit set by
chmod), and if you call
this script is still run by
/bin/sh which is typically
dash on Linux. So be careful on how you run your code.
There is a way to avoid your problem. Change your script to use:
ch=`echo "b_d" | sed 's/_/\\\\\\\\_/'`
printf '%s\n' $ch
This way, you avoid the problems with
echo that have been introduced in 1989 with
printf does still not solve all problems since there are many buggy implementations, but the usual problems that arise from backslashes in the arguments are not affected by