How to run shell commands from the Scala REPL
By Alvin Alexander. Last updated: June 27 2019
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.4, “How to run a shell command from the Scala REPL.”
Problem
You want to be able to run a shell command from within the Scala REPL, such as listing the files in the current directory.
Solution
Run the command using the :sh
REPL command, then print the output. The following example shows how to run the Unix ls -al
command from within the REPL, and then show the results of the command: