How to pass arguments to bash chunk defined as variables in R?


I want to use variables defined in R environment as an argument in the bash chunk. Is there any way to do that?

For instance, in the examples below I want to use R to define the path to the script and pass it as an argument to bash:

path_to_script <- glue("{here::here()}/bash_scripts/")

bash  **path_to_script**


I'm assuming you're on a linux machine since you're using shell scripts, and in my experience the easiest way is using the system() function.

For example:


will output the date to the console. If you like to capture the output text to an R variable:

var = system("date", intern = T)

So if you like to run a script with some arguments can you just do:

arg1 = 10
command = sprintf("path/to/script/ -x %i", arg1)


Hope this helps,

Thank you, @pieterjanvc!
Why don't I think of this simple solution myself?

Just in case, I'm on Windows. And I'm using shell script because it works and I don't know alternatives))

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.