Last chance! 50% off unlimited learning
Sale ends in
shQuote(string, type = c("sh", "csh", "cmd"))
"cmd"
refers to the Windows NT shell, and is the
default under Windows.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 bash
, ksh
and
zsh
. The other type of quoting is for the C-shell (csh
and
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.
windows
The Windows shell supports only double quoting. All this
implementation does is to surround the string by double quotes and
escape internal double quotes by a backslash. As Windows path names
cannot contain double quotes, this makes shQuote
safe for use
with file paths in system
, and with shell
if the default shell is used.
It will usually be safe to use shQuote
to quote arguments of a
command, but because system
does not use a shell,
interpretation of quoted arguments is done by the run-time code of the
executable. This may depend on the compiler used: Microsoft's rules
for the C run-time are given at
https://msdn2.microsoft.com/en-us/library/ms880421.aspx.
Quotes
for quoting R code. sQuote
for quoting English text.
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 <- paste("use the", sQuote("-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")))
Run the code above in your browser using DataLab