How to run R scripts from the command line


Running R scripts from the command line can be a powerful way to:

  • Automate your R scripts
  • Integrate R into production
  • Call R through other tools or systems
There are basically two Linux commands that are used. The first is the command, Rscript, and is preferred. The older command is R CMD BATCH. You can call these directly from the command line or integrate them into a bash script. You can also call these from any job scheduler.
Note, these are R related tools. The RStudio IDE does not currently come with tools that enhance or manage the Rscript and R CMD BATCH functions. However, there is a shell built into the IDE and you could conceivably call these commands from there.
The alternative to the using the Linux command line is to use the source() function inside of R. The source function will also call a script, but you have to be inside an R session to use it.
Here are a few external references to the Rscript and R CMD BATCH commands:


  • Avatar

    n my Linux-based web-hosting when I am running the command Rscript, it's saying the command does not exist, any resolutions pl?

  • Avatar
    Cecil Singh

    Hi Gaurav,

    That does seem strange. Rscript is provided when you install R. It may be worth checking that R is correctly installed on your system. If this is the case, ensure that PATH refers to the correct Rscript. You can run something like the below to find this:

    which Rscript

    Note that this should be within the same directory that exists when you run:

    which R

    Best of luck! I hope this helps point you in the right direction.