Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Escaping shell characters in a string



If I set $fred:

% fred="a [ a * a ? a \ a"

If I expand fred while still on the command line:

% echo "$fred"<Tab>

Then all the shell space/glob characters are escaped:

% echo a\ \[\ a\ \*\ a\ \?\ a\ \\\ a

How can I get this quoting/escaping behavior within a zsh function?
That is, given a variable, how can I best escape all of the special
shell characters within?  Is there a variable modifier I can use that
automatically escapes all special characters?

The best I've been able to come up with is:

escaped="$fred"
escaped="${escaped// /\\ }"
escaped="${escaped//\[/\\[}"
escaped="${escaped//\*/\\*}"
...

I know there has to be a better way.

Thanks,

Steve

Attachment: pgpnp4yL3yq0d.pgp
Description: PGP signature



Messages sorted by: Reverse Date, Date, Thread, Author