Sample Header Ad - 728x90

printf in Zsh does not shell escape exclamation mark

5 votes
1 answer
426 views
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?
Asked by user137369 (584 rep)
May 17, 2024, 04:28 PM
Last activity: May 17, 2024, 06:37 PM