Script programming with scala?

is there any book for this content oriented? or is scala suitable for working as a script language? thank you.

There is Ammonite and Li Haoyi’s book Hands on Scala.


Also scala-cli works well for scripts.


FYI: If you are new to Scala, it is important to understand there is a large difference between wanting to write “scripts” with Scala, and fundamentally learning Scala itself.

With most scripting languages, the focus is on letting “non-software-engineers” (like Admins and DevOps) get things done. This means the scripting languages aren’t very principled and involve lots of “boilerplate” and “hacking” which “keeps things simpler”. Of course, scripts grow over time and eventually become un-simple. And that’s when all the headaches of that scripting language get magnified into unmanageable and unmaintainable complexity.

Scala has almost exactly the opposite design goals. This means “scripting” is NOT an ideal approach to learning Scala itself. However, it IS an ideal approach to taming the complexity inherent in other scripting languages.

If you are learning Scala, then definitely head in the Ammonite direction as Li does a fantastic job guiding you through all sorts of “scripting” priority things.

However, if you already know Scala and you are attempting to write things that fall along the more principled FP direction, then Scala-CLI is likely a better choice.

Again, this is an OPINION. So, be sure to value it exactly how much it cost you. :stuck_out_tongue:


It depends on what you mean with “suitable”.

If you already know Scala and want to write some small scripts using the language for your personal use, then tools like Ammonite and scala-cli are great.

If you are trying to learn the language through small scripts, then I would again recommend scala-cli and sooner than latter start using sbt to write bigger programs.

If you are thinking on creating scripts that can be easily shared with other people then I would say no.
The advantage of most scripting languages is that most Linux distros already include them by default and people is already used to managing them.
Whereas for Scala, you would require folks to install a JDK (not even just a JRE), and then extra tools.

Creating native images may be a great middle step but is still relative unexplored territory.


A lot of common tasks that scripts need to do are well supported by this library, by Haoyi Li:

GitHub - com-lihaoyi/os-lib: OS-Lib is a simple, flexible, high-performance Scala interface to common OS filesystem and subprocess APIs

I think Ammonite may have it on the classpath by default?

Anyway, scala-cli makes adding it as a dependency a one-line addition to your script. Here’s a sample scala-cli script that uses it:

#!/usr/bin/env -S scala-cli shebang

//> using scala "3"
//> using lib "com.lihaoyi::os-lib:0.8.1"

os.write(os.pwd/"file.txt", "hello\nthere\n")

Li Haoyi’s ammonite can now be used with scala 3 using:

> scala-cli repl --amm

I have been using that recently to try something more like data exploration,

You have some data on the web that you want to understand. You download libraries to fetch it and parse it, and then you look at it that with the ammonite browse command - which is a pager so it does not kill your shell). You then think about it, transform the data, perhaps download new libs with import $ivy.... that you need to transform it again, browse the results, etc… All the while you then write up your results into what may end up becoming a script or a scala class…

5 years ago I wrote up a similar experiment using ammonite to explore the semantic web, and wrote this up here:

The scripts I think no longer work, and would need to be updated to Scala 3.

Right now I am downloading CSV files, then looking at the result data, filtering it, group it, and slowly understanding what it was meant to mean. Then I write what I discover up in a script, and continue the next day from where I left off…

I have a feeling that the scala-cli folks are just starting to grok this use case, because scala-cli repl without the --amm flag has no browse tool, and they seem to want to go to all imports being declared in advance. That is too static a thinking. I am told this type of use case is what big data exploration tools such as help with too. Those are much heavier though.