Rev 46526 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/shQuote.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\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, and is thedefault under Windows.}}\details{The default type of quoting supported under Unix-alikes is that forthe Bourne shell \code{sh}. If the string does not contain singlequotes, we can just surround it with single quotes. Otherwise, thestring is surrounded in double quotes, which suppresses all specialmeanings of metacharacters except dollar, backquote and backslash, sothese (and of course double quote) are preceded by backslash. Thistype of quoting is also appropriate for \code{bash}, \code{ksh} and\code{zsh}.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.#ifdef windowsThe Windows shell supports only double quoting. All thisimplementation does is to surround the string by double quotes andescape internal double quotes by a backslash. As Windows path namescannot contain double quotes, this makes \code{shQuote} safe for usewith file paths in \code{\link{system}}, and with \code{\link{shell}}if the default shell is used.It will usually be safe to use \code{shQuote} to quote arguments of acommand, but because \code{\link{system}} does not use a shell,interpretation of quoted arguments is done by the run-time code of theexecutable. This may depend on the compiler used: Microsoft's rulesfor the C run-time are given at\url{http://msdn2.microsoft.com/en-us/library/ms880421.aspx}.#endif}\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{Quotes} for quoting \R code.\code{\link{sQuote}} for quoting English 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 <- paste("use the", sQuote("-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}