Always enclose strings and scripts in single quotes (
') unless you need to use double quotes (
") to make the shell interpret it. See https://mywiki.wooledge.org/Quotes. By using double quotes you are inviting the shell in and so putting yourself into "escaping" hell where you have to escape characters within the string for shell to use up first and then escape them again for the tool to use so you need to add multiple layers of escapes instead of 1. Just don't do that, use single quotes instead:
$ printf '%s\n' ''\''\"?!'
$ printf '%s\n' ''\''\"?!' | tr ''\''\\"?!' '01234'
The same is true when you define your variable
char. Instead of having all those backslashes:
just single-quote the string properly:
$ export char=''\''\"?!'
$ printf '%s\n' "$char"
$ printf '%s\n' "$char" | tr ''\''\\"?!' '01234'
In the above all you need to know is that to get a
' inside a
'-enclosed string in shell is
'\'' and you need to escape the backslash in the
tr so that
tr knows to treat it as a literal backslash rather than an escape for the subsequent