Run shell commands from Swift
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Build Status Platforms Swift Package Manager Compatible Swift 4


Represents a shell command and provides a means to run and redirect the output of a shell command.

let command = ShellCommand()
try["/bin/echo", "hello", "world"])
// Run the next command in /somewhere/else
command.currentDirectoryPath = "/somewhere/else""echo \"hi there\" > a_file")[ "cat", "/somewhere/else/a_file" ])

// Implement your own class, or use CapturedIO, to catch what's sent to
// "writeMessage" = CapturedIO()
try["/usr/bin/git", "add", "-A"])
try["/usr/bin/git", "commit", "-m", "Broke everything"])
print( "git said: \(io.stuout)" )

Pass run a String and it'll run it with bash:

// Send "hello world" to STDERR
try"echo \"hello world\" >&2")

Pass run a [String] and it'll run the command directly:

// Send scary characters to STDOUT without shell interpolation
try["/bin/echo", "hello", "; rm /* @%&()##$$ scary!"])
// Outputs: hello ; rm /* @%&()##$$ scary!

Run one-offs with convenient static methods:

// Initialize and call run with a single string. Command is passed to
// /bin/bash -c. Handy both for shorthand and for redirects.
try"echo \"hello world!\" > /tmp/somefile")

// This will send to STDERR.
try"echo \"hello world!\" >&2")

// Add a custom ConsoleIOProtocol object to capture output
let output = CapturedIO()
try"echo \"hello world!\" >&2", io: output)
print( output.stderr ) // Prints "hello world!"

Developed for and tested on MacOS and Linux.


This is a Swift Package Manager (SPM) package. Include this is your SPM file:

.package(url: "", from: "1.0.0"),

Then import ShellCommand at the top of your file.

import ShellCOmmand"echo \"Hello World!\"")


Get the source

git clone
swift package update
swift package generate-xcodeproj

And install sourcery if you don't already have it.


open -a ShellCommand.xcodeproj


swift test

Make tests for Linux and commit

git commit

See Also

SwiftShell. SwiftCommand handles things a little differently and is a bit lighter-weight.