simple (and stupid) shell scripting question

Christian Weisgerber naddy at
Tue Feb 16 18:31:49 UTC 2010

Nerius Landys <nlandys at> wrote:

> Is there a function, or command line utility, to "escape" a string,
> making it suitable to be input on the command line?  For example, this
> "escape" utility would take a input of "te st" and create an output of
> "te\ st".  Other things such as quotes and single quotes would be
> handled as well.

You can use something like this:  sed 's/[^A-Za-z_0-9]/\\&/'

Perl has a quotemeta() function and \Q escape sequence for this
purpose, e.g.:

$ perl -e '$_="te st"; print "\Q$_\E\n"'
te\ st

Christian "naddy" Weisgerber                          naddy at

More information about the freebsd-questions mailing list