ID EN
Miscellaneous

shQuote

R Base 3.6.2

Quote a string to be passed to an operating system shell.

Syntax

R
shQuote(string, type = c("sh", "csh", "cmd", "cmd2"))

Arguments

Parameter Description
string a character vector, usually of length one.
type character: the type of shell quoting. Partial matching is supported. "cmd" and "cmd2" refer to the Windows shell. "cmd" is the default under Windows.

Details

The default type of quoting supported under Unix-alikes is that for the Bourne shell 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). A

Examples

Example
R
# NOT RUN {
test <- "abc$def`gh`i\\j"
cat(shQuote(test), "\n")
# }
# NOT RUN {
system(paste("echo", shQuote(test)))
# }
# NOT RUN {
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)))
# }
# NOT RUN {
cat(shQuote(tryit, type = "csh"), "\n")

## Windows-only example, assuming cmd.exe:
perlcmd <- 'print "Hello World\\n";'
# }
# NOT RUN {
shell(shQuote(paste("perl -e",
                    shQuote(perlcmd, type = "cmd")),
              type = "cmd2"))
# }

See Also

Quotes for quoting R code. sQuote for quoting English text.