Library for running shell scripts and other executables from swift code.
The library is distributed as a swift package.
// in your Package.swift, add package dependency:
.package(url: "https://github.com/darrarski/swift-shell.git", from: "0.1.0"),
// and add the library as a dependency to your target:
.product(name: "SwiftShell", package: "swift-shell"),
The code is dressed in documentation comments that explain the purpose of each variable and function. The examples described below and the library unit tests explain how to use it. You can also check out the auto-generated documentation hosted on SwiftPackageIndex.
Run bash script, send input, and retrieve the script's output:
let process = ShellProcess(.bash("IFS= read -r NAME; echo Hello, $NAME!"))
try await process.run()
try await process.send(input: "Swift\n".data(using: .utf8)!)
let output = try await process.output()!
print(String(data: output, encoding: .utf8)!) // Hello, Swift!
Check out the examples included in this repository:
Send input and stream bash script's output.
Run example:
$ Examples/run.sh -- InputOutputExample
Stream and decorate bash script's output.
Run example:
$ Examples/run.sh -- DecorateOutputExample
- Use Xcode (≥15.4).
- Clone the repository or create a fork & clone it.
- Open
SwiftShell.xcworkspace
in Xcode. - Use the
SwiftShell
scheme for building and testing the library. - Use other schemes to build or test examples.
- If you want to contribute, create a pull request containing your changes or bug fixes. Make sure to include tests for new/updated code.
I would love to hear if you like my work. I can help you apply any of the solutions used in this repository in your app too! Feel free to reach out to me, or if you just want to say "thanks", you can buy me a coffee.
Copyright © 2024 Dariusz Rybicki Darrarski
License: MIT