Rev 28722 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{shQuote}\alias{shQuote}\title{Quote Strings for Use in OS Shells}\description{Quote a string to be passed to an operating system shell.}\usage{shQuote(string, type = c("sh", "csh", "cmd"))}\arguments{\item{string}{a character vector, usually of length one.}\item{type}{character: the type of shell. Partial matching issupported. \code{"cmd"} refers to the Windows NT shell.}}\details{The default type of quoting supported is that for the Bourne shell\code{sh}. If the string does not contain single quotes, we can justsurround it with single quotes. Otherwise, the string is surrounded indouble quotes, which suppresses all special meanings of metacharactersexcept dollar, backquote and backslash, so these (and of course doublequote) are preceded by backslash. This type of quoting is alsoappropriate for \code{ksh} 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 singlequotes, we can just surround it with single quotes. If it doescontain single quotes, we can use double quotes provided it does notcontain dollar or backquote (and we need to escape backslash,exclamation mark and double quote). As a last resort, we need tosplit the string into pieces not containing single quotes and surroundeach with single quotes, and the single quotes with double quotes.The Windows shell supports only double quoting. All thisimplementation does is to surround by double quotes and escapeinternal double quotes by a backslash.}\references{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}}\seealso{\code{\link{sQuote}} for quoting in text.}\examples{test <- "abc$def`gh`i\\j"cat(shQuote(test), "\n")\dontrun{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")\dontrun{system(paste("echo", shQuote(tryit)))}cat(shQuote(tryit, type="csh"), "\n")## Windows-only example.perlcmd <- 'print "Hello World\\n";'\dontrun{shell(paste("perl -e", shQuote(perlcmd, type="cmd")))}}\keyword{utilities}