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}:

ls *.Rmd | head -n 5
## 00-authors.Rmd
## 01-installation.Rmd
## 02-overview.Rmd
## 03-basics.Rmd
## 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.,

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

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:

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.