Reputation: 4480
In zsh, what is the most efficient and/or most concise way to backslash escape characters in a string for use as a path argument?
I do not want to enclose the whole path in any type of quotes.
e.g., if I have a zsh string a & b
, I want to get a\ \&\ b
.
I do not want: 'a & b'
, "a & b"
, $'a & b'
, etc. (those can be obtained via various q
parameter expansion flags; none of the q
flags that I found backslash escape spaces or ampersands, so they won't work for me)
I haven't been able to find a way to do this concisely, either via parameter expansion or via any other simple command, other than to use a regex replace parameter expansion (of the form ${name//pattern/repl}
), but that would require me to learn all the characters that must be escaped, and would require me to update the expansion if there are any changes.
Upvotes: 1
Views: 94