SwiftShell 2.0 Readme

I finally got around to updating the SwiftShell 2.0 readme with some actual usage instructions:


An OS X Framework for command line scripting in Swift.


Put this at the beginning of each script file:

Run commands

Print output

Runs a shell command just like you would in the terminal. If the command returns with a non-zero exit code it will throw a ShellError.

The name may seem a bit cumbersome, but it explains exactly what it does. SwiftShell never prints anything without explicitly being told to.


Similar to $(cmd) in bash, this just returns the output from the command as a string, ignoring any errors.


Launch a command and continue before it’s finished. You can process standard output and standard error, and optionally wait until it’s finished and handle any errors.

If you read all of command.stderror or command.stdout it will automatically wait for the command to finish running. You can still call finish() to check for errors.


The 3 run functions above take 2 different types of parameters:

(executable: String, _ args: Any …)

If the path to the executable is without any /, SwiftShell will try to find the full path using the which shell command.

The array of arguments can contain any type, since everything is convertible to strings in Swift. If it contains any arrays it will be flattened so only the elements will be used, not the arrays themselves.

(bash bashcommand: String)

These are the commands you normally use in the Terminal. You can use pipes and redirection and all that good stuff. Support for other shell interpreters can easily be added.


main.stdout is for normal output and main.stderror for errors:


Use main.stdin to read from standard input:


So what else can main do? It is the only global value in SwiftShell and contains all the contextual information about the outside world:

Everything is mutable, so you can set e.g. the text encoding or reroute standard error to a file.


Print line numbers

Launched with e.g. cat long.txt | print_linenumbers.swift or print_linenumbers.swift long.txt this will print the line number at the beginning of each line.


  • In the Terminal, go to where you want to download SwiftShell.
  • Run

  • Copy/link Misc/swiftshell to your bin folder or anywhere in your PATH.

  • To install the framework itself, either:
    • run xcodebuild install from the project’s root folder. This will install the SwiftShell framework in ~/Library/Frameworks.
    • or run xcodebuild and copy the resulting framework from the build folder to your library folder of choice. If that is not “~/Library/Frameworks”, “/Library/Frameworks” or a folder mentioned in the $SWIFTSHELL_FRAMEWORK_PATH environment variable then you need to add your folder to $SWIFTSHELL_FRAMEWORK_PATH.