sh differs from bash and behaves different.
I guess that sh is really dash.
BTW: there is different behavior with this command if you check different shells.
bosh, dash, mksh, zsh as well as ksh on Solaris print one backslash
bash and 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 behavior.
For 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
#!/bin/bash
and that is executable (x bit set bychmod), and if you call
sh myscript
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 bash. 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 printf.
shis notbash. Also, if you run$SHELL foo.shthe shebang line is ignored. Please show us exactly what you are running. – l0b0 Aug 25 '18 at 09:25