In Zsh 5.9, we can use printf to shell escape a string:
$ printf '%q' 'One! Two'
One\!\ Two
This produces the correct output of escaping the ! and the space. Now let’s make it as a script:
#!/bin/zsh
printf '%q' "${1}"
Now if we run it, the ! is not escaped but the space is:
$ ./my-script 'One! Two'
One!\ Two
If I change the script to /bin/bash (version 3.2), it correctly escapes the ! and space.
Is this a Zsh bug, or is there some subtle detail I’m missing?