\HeaderA{shQuote}{Quote Strings for Use in OS Shells}{shQuote}
\keyword{utilities}{shQuote}
\begin{Description}\relax
Quote a string to be passed to an operating system shell.
\end{Description}
\begin{Usage}
\begin{verbatim}
shQuote(string, type = c("sh", "csh", "cmd"))
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{string}] a character vector, usually of length one.
\item[\code{type}] character: the type of shell.  Partial matching is
supported.  \code{"cmd"} refers to the Windows NT shell, and is the
default under Windows.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The default type of quoting supported under Unix-alikes is that for
the Bourne shell \code{sh}.  If the string does not contain single
quotes, we can just surround it with single quotes.  Otherwise, the
string is surrounded in double quotes, which suppresses all special
meanings of metacharacters except dollar, backquote and backslash, so
these (and of course double quote) are preceded by backslash.  This
type of quoting is also appropriate for \code{ksh}, \code{zsh} and
\code{bash}.

The other type of quoting is for the C-shell (\code{csh} and
\code{tcsh}).  Once again, if the string does not contain single
quotes, we can just surround it with single quotes.  If it does
contain single quotes, we can use double quotes provided it does not
contain dollar or backquote (and we need to escape backslash,
exclamation mark and double quote).  As a last resort, we need to
split the string into pieces not containing single quotes and surround
each with single quotes, and the single quotes with double quotes.
\end{Details}
\begin{References}\relax
Loukides, M. et al (2002)
\emph{Unix Power Tools} Third Edition.  O'Reilly.  Section 27.12.

\url{http://www.mhuffman.com/notes/dos/bash_cmd.htm}
\end{References}
\begin{SeeAlso}\relax
\code{Quotes} for quoting \R{} code.

\code{\LinkA{sQuote}{sQuote}} for quoting English text.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
test <- "abc$def`gh`i\j"
cat(shQuote(test), "\n")
## Not run: system(paste("echo", shQuote(test)))
test <- "don't do it!"
cat(shQuote(test), "\n")

tryit <- "use the `-c' switch\nlike this"
cat(shQuote(tryit), "\n")
## Not run: system(paste("echo", shQuote(tryit)))
cat(shQuote(tryit, type="csh"), "\n")

## Windows-only example.
perlcmd <- 'print "Hello World\n";'
## Not run: shell(paste("perl -e", shQuote(perlcmd, type="cmd")))
\end{ExampleCode}
\end{Examples}

