15.4 Execute Shell scripts

You can run Shell scripts via the bash or sh or zsh engine, depending on which shell you prefer. Below is a bash example, with the chunk header ```{bash}:

  1. ls *.Rmd | head -n 5
  1. ## 00-authors.Rmd
  2. ## 01-installation.Rmd
  3. ## 02-overview.Rmd
  4. ## 03-basics.Rmd
  5. ## 04-content.Rmd

Please note that bash is invoked with the R function system2(). It will ignore profile files like ~/.bash_profile and ~/.bash_login, in which you may have defined command aliases or modified environment variables like the PATH variable. If you want these profile files to be executed just like when you use the terminal, you may pass the argument -l to bash via engine.opts, e.g.,

  1. ```{bash, engine.opts='-l'}
  2. echo $PATH
  3. ```

If you want to enable the -l argument globally for all bash chunks, you may set it in the global chunk option in the beginning of your document:

  1. knitr::opts_chunk$set(engine.opts = list(bash = "-l"))

You can also pass other arguments to bash by providing them as a character vector to the chunk option engine.opts.