MiniScript on the Command Line

You can use MiniScript on the command line, and even use it to write clean, readable shell scripts!

Download & Installation

Download and unzip the appropriate binary for your system:

Then install it (or a symbolic link to it) somewhere in your path. On Mac and Linux, /usr/local/bin/ is a standard choice.

The distribution includes a lib folder which you'll want to keep next to the executable, and also a demo folder of short programs you can keep wherever. See the included README.md file for more details.

Usage

The miniscript command by itself invokes an interactive (read/eval/print) loop. You can enter any MiniScript expression, press Return, and immediately see the answer. This is handy for quick calculations or experimentation.

$ miniscript
MiniScript 
Command-Line (Unix) v1.3; language v1.6.2 (Feb 18 2024)
> 2+2
4

You can also enter functions, loops, if statements, etc.; when you have one of these open, the prompt changes from > to >>> to indicate that more input is needed.

> for i in range(1,5)
>>> print "Spam " * i
>>> 

The code will be executed when the block closer (end for in this case) has been entered.

To exit interactive MiniScript, press Control-D (i.e. terminate input) or enter exit.

You can also invoke MiniScript with a single command (which could be multiple statements separated by semicolons), using the -c option. MiniScript will execute that command and immediately exit.

$ miniscript -c 'print 2^12'
4096

Or, you can execute miniscript with the path to a MiniScript script file. The script file will be executed, and MiniScript will exit when done. Note that in this mode, MiniScript skips the first line if it starts with "#!". This allows you to write shell scripts that can be executed directly.

#!/usr/local/bin/miniscript
print "The current directory is: " + file.curdir
print "And my arguments are: " + shellArgs

Command-line MiniScript has a few other command-line options; use miniscript -? to see what they are.

Extra Intrinsics

In addition to the standard MiniScript intrinsic methods (as seen in the Quick Reference and User's Manual), the command-line version of MiniScript provides the following:

exit(resultCode=0)quits the MiniScript session
input(prompt="")prompts the user for input, and returns the entered string
shellArgsreturns command-line arguments given with script invocation, as a list; shellArgs[0] is the script itself
envreturns the shell environment variables, as a map; assignment to this map adds/updates environment variables
exec(cmd, timeout=30)executes a shell command (timeout is in seconds)
filemodule of file-related methods (see below)
importimports another script (see the wiki for details)

file returns a map that simply groups together a bunch of methods for manipulating files.

file.curdirreturns the current working directory
file.setdir(path)changes the current working directory
file.makedir(path)creates a new directory
file.children(path)returns list of files within the given directory
file.name(path)returns the file name from the given path
file.parent(path)returns the path to the directory containing the given path
file.child(parentPath, childName)combines the given directory path and file name, returning path to the child
file.exists(path)returns true if a file exists at the given path
file.copy(oldPath, newPath)copies a file to a new location
file.move(oldPath, newPath)moves/renames a file
file.delete(path)deletes a file
file.info(path)gets a map of details about a file including size, isDirectory, date, and (full) path
file.readLines(path)returns full contents of a text file as a list of strings
file.writeLines(path, data)writes out the given list as a text file, one line per list element
file.open(path, mode="rw+")opens or creates a file; mode can be "rw+" (open for reading/updating, creating if it doesn't exist), or any standard fopen mode; returns a file handle object (see below)

Finally, when you use file.open to open a file, what you get back is a little object that allows for reading and writing from that file. This object has the following methods.

.closecloses the open file
.isOpenreturns true if the file is open, false if closed
.write(s)writes the given string to the file
.writeLine(s)writes the given string, followed by a carriage return
.readreads the rest of the file, returning it as a string
.readLinereads the next line of the file
.positionreturns current read/write position in the file
.atEndreturns true at the end of the file, false otherwise

Source Code

Source code for the MiniScript core, and this command-line wrapper is available in our GitHub repo. Contributions and suggestions are very welcome!

Version History

1.3Feb 18 2024
  • Updated core language to MiniScript 1.6.2 (see release notes)
  • Added an exec intrinsic, to execute a shell command
  • Added a -i command-line flag to enter interactive mode after running the given script
  • Output from print is flushed right away, preventing visual bugs if you suppress the line break and follow it with an input call
  • fixed an issue that could cause miniscript to fail on a source file with a very long line
  • Changed build instructions from hand-written makefile to CMake
1.2.1Jun 30 2023
  • Updated core language to MiniScript 1.6.1 (see release notes)
  • fixed an error which could sometimes, at random, cause the command-line REPL to be nonresponsive
1.2Feb 06 2023
  • Updated core language to MiniScript 1.6 (see release notes)
  • upon launch, now adds MS_SCRIPT_DIR and MS_EXE_DIR to the environment variables, as well as MS_IMPORT_PATH if not already defined
  • added import intrinsic, searching directories in MS_IMPORT_PATH for the specified import module
  • now ships with a set of 11 standard import modules, including date/time functions, JSON and tab-separated value converters, misc. utilities, and VTxxx terminal control
1.1Dec 18 2021Updated core language to 1.5.1 (no changes to host environment).
1.1Jan 16 2021Added env to provide access to environment variables.
1.0Sep 13 2019First public release for Mac, Windows, and Linux.

MiniScript Home Page