Skip to contents

Invoke a command in the system command-line shell

Usage

shell(
  command,
  args = character(),
  env = NULL,
  wd = getwd(),
  print = TRUE,
  stderrFile = NULL,
  stdoutFile = NULL,
  stderrToStdout = FALSE,
  returnStdout = FALSE
)

Arguments

command

character(1). Name of program to run.

args

character. Arguments passed to command.

env

character or NULL. Environment variables of the child process. If NULL, the parent environment is inherited. To append new environment variables to the ones set in the current process, specify "current" in env, without a name, and the appended ones with names. The appended ones can overwrite the current ones.

wd

character(1). Working directory path inside process.

print

logical(1). Whether to print (echo) the commands to the console.

stdoutFile, stderrFile

character(1) or NULL. File path to log stdout and/or stderr. Disabled when set NULL.

stderrToStdout

logical(1). Whether to redirect standard error (stderr) to standard output (stdout). Similar to 2>&1 in POSIX or &> in Bash.

returnStdout

logical(1). Whether to return stdout as a character vector, split by "\n".

Value

Invisible list. Contains named elements: "status", "stdout", "stderr", "timeout".

Note

Updated 2023-11-04.

See also

Examples

x <- shell(
    command = "printf",
    args = c("%s\n", "hello", "world"),
    print = TRUE
)
#> → Shell subprocess: "printf %s\n hello world"
#> hello
#> world
print(x)
#> $status
#> [1] 0
#> 
#> $stdout
#> [1] "hello\nworld\n"
#> 
#> $stderr
#> [1] ""
#> 
#> $timeout
#> [1] FALSE
#>