Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 16 Feb 2010 18:31:46 +0000 (UTC)
From:      naddy@mips.inka.de (Christian Weisgerber)
To:        freebsd-questions@freebsd.org
Subject:   Re: simple (and stupid) shell scripting question
Message-ID:  <hleoai$2nmq$1@lorvorc.mips.inka.de>
References:  <560f92641002142207w7eade79fr6a4f40ae5b92f4b9@mail.gmail.com> <hlbmrs$1fc8$1@lorvorc.mips.inka.de> <560f92641002151216l6b8e97f4u886ac8e3b86d231e@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Nerius Landys <nlandys@gmail.com> 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@mips.inka.de




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?hleoai$2nmq$1>