{"id":924,"url":"https://github.com/kareman/SwiftShell","last_synced_at":"2025-08-06T13:32:00.829Z","repository":{"id":20095971,"uuid":"23365402","full_name":"kareman/SwiftShell","owner":"kareman","description":"A Swift framework for shell scripting.","archived":false,"fork":false,"pushed_at":"2020-09-25T20:45:13.000Z","size":3238,"stargazers_count":1038,"open_issues_count":12,"forks_count":88,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-12-07T15:03:43.822Z","etag":null,"topics":["command-line","script","shell","swift","swift-library"],"latest_commit_sha":null,"homepage":"https://kareman.github.io/SwiftShell","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kareman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-26T20:16:32.000Z","updated_at":"2024-12-02T07:09:53.000Z","dependencies_parsed_at":"2022-08-03T03:46:12.139Z","dependency_job_id":null,"html_url":"https://github.com/kareman/SwiftShell","commit_stats":null,"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kareman%2FSwiftShell","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kareman%2FSwiftShell/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kareman%2FSwiftShell/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kareman%2FSwiftShell/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kareman","download_url":"https://codeload.github.com/kareman/SwiftShell/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228639089,"owners_count":17949931,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["command-line","script","shell","swift","swift-library"],"created_at":"2024-01-05T20:15:34.749Z","updated_at":"2024-12-09T14:30:44.239Z","avatar_url":"https://github.com/kareman.png","language":"Swift","readme":"Run shell commands | [Parse command line arguments](https://github.com/kareman/Moderator) | [Handle files and directories](https://github.com/kareman/FileSmith)\n\n---\n\nSwift 5.1 - 5.3 | [Swift 4](https://github.com/kareman/SwiftShell/tree/Swift4) | [Swift 3](https://github.com/kareman/SwiftShell/tree/Swift3) | [Swift 2](https://github.com/kareman/SwiftShell/tree/Swift2)\n\n\u003cp align=\"center\"\u003e\n\t\u003cimg src=\"https://raw.githubusercontent.com/kareman/SwiftShell/master/Misc/logo.png\" alt=\"SwiftShell logo\" /\u003e\n\u003c/p\u003e\n\n![Platforms](https://img.shields.io/badge/platforms-macOS%20%7C%20Linux++-lightgrey.svg) \u003ca href=\"https://swift.org/package-manager\"\u003e\u003cimg src=\"https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat\" alt=\"Swift Package Manager\" /\u003e\u003c/a\u003e [![Carthage compatible](https://img.shields.io/badge/carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) \u003ca href=\"https://twitter.com/nottoobadsw\"\u003e\u003cimg src=\"https://img.shields.io/badge/contact-@nottoobadsw-blue.svg?style=flat\" alt=\"Twitter: @nottoobadsw\" /\u003e\u003c/a\u003e\n\n# SwiftShell\n\nA library for creating command-line applications and running shell commands in Swift. \n\n#### Features\n\n- [x] run commands, and handle the output.\n- [x] run commands asynchronously, and be notified when output is available.\n- [x] access the context your application is running in, like environment variables, standard input, standard output, standard error, the current directory and the command line arguments.\n- [x] create new such contexts you can run commands in.\n- [x] handle errors.\n- [x] read and write files.\n\n#### See also\n\n- [API Documentation](https://kareman.github.io/SwiftShell).\n- A [description](https://www.skilled.io/kare/swiftshell) of the project on [skilled.io](https://www.skilled.io).\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n#### Table of Contents\n\n- [Example](#example)\n    - [Print line numbers](#print-line-numbers)\n    - [Others](#others)\n- [Context](#context)\n    - [Main context](#main-context)\n    - [Example](#example-1)\n- [Streams](#streams)\n    - [WritableStream](#writablestream)\n    - [ReadableStream](#readablestream)\n    - [Data](#data)\n- [Commands](#commands)\n    - [Run](#run)\n    - [Print output](#print-output)\n    - [Asynchronous](#asynchronous)\n    - [Parameters](#parameters)\n    - [Errors](#errors)\n- [Setup](#setup)\n  - [Stand-alone project](#stand-alone-project)\n  - [Script file using Marathon](#script-file-using-marathon)\n  - [Swift Package Manager](#swift-package-manager)\n  - [Carthage](#carthage)\n  - [CocoaPods](#cocoapods)\n- [License](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n\n## Example\n\n#### Print line numbers\n\n```swift\n#!/usr/bin/env swiftshell\n\nimport SwiftShell\n\ndo {\n\t// If there is an argument, try opening it as a file. Otherwise use standard input.\n\tlet input = try main.arguments.first.map {try open($0)} ?? main.stdin\n\n\tinput.lines().enumerated().forEach { (linenr,line) in \n\t\tprint(linenr+1, \":\", line) \n\t}\n\n\t// Add a newline at the end.\n\tprint(\"\")\n} catch {\n\texit(error)\n}\n```\n\nLaunched 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.\n\n#### Others\n\n- [Test the latest commit (using make and/or Swift).][testcommit]\n- [Run a shell command in the middle of a method chain](https://nottoobadsoftware.com/blog/swiftshell/combine-markdown-files-and-convert-to-html-in-a-swift-script/).\n- [Move files to the trash](https://nottoobadsoftware.com/blog/swiftshell/move-files-to-the-trash/).\n\n[testcommit]: https://github.com/kareman/testcommit/blob/master/Sources/main.swift\n\n## Context\n\nAll commands (a.k.a. [processes][]) you run in SwiftShell need context: [environment variables](https://en.wikipedia.org/wiki/Environment_variable), the [current working directory](https://en.wikipedia.org/wiki/Working_directory), standard input, standard output and standard error ([standard streams](https://en.wikipedia.org/wiki/Standard_streams)).\n\n```swift\npublic struct CustomContext: Context, CommandRunning {\n\tpublic var env: [String: String]\n\tpublic var currentdirectory: String\n\tpublic var stdin: ReadableStream\n\tpublic var stdout: WritableStream\n\tpublic var stderror: WritableStream\n}\n```\n\nYou can create a copy of your application's context: `let context = CustomContext(main)`, or create a new empty one: `let context = CustomContext()`. Everything is mutable, so you can set e.g. the current directory or redirect standard error to a file.\n\n[processes]: https://en.wikipedia.org/wiki/Process_(computing)\n\n#### Main context\n\nThe global variable `main` is the Context for the application itself. In addition to the properties mentioned above it also has these:\n\n- `public var encoding: String.Encoding`\nThe default encoding used when opening files or creating new streams.\n- `public let tempdirectory: String`\nA temporary directory you can use for temporary stuff.\n- `public let arguments: [String]`\nThe arguments used when launching the application.\n- `public let path: String`\nThe path to the application.\n\n`main.stdout` is for normal output, like Swift's `print` function. `main.stderror` is for error output, and `main.stdin` is the standard input to your application, provided by something like `somecommand | yourapplication` in the terminal.\n\nCommands can't change the context they run in (or anything else internally in your application) so e.g. `main.run(\"cd\", \"somedirectory\")` will achieve nothing. Use `main.currentdirectory = \"somedirectory\"` instead, this changes the current working directory for the entire application.\n\n#### Example\n\nPrepare a context similar to a new macOS user account's environment in the terminal (from [kareman/testcommit][testcommit]):\n\n```swift\nimport SwiftShell\nimport Foundation\n\nextension Dictionary where Key:Hashable {\n\tpublic func filterToDictionary \u003cC: Collection\u003e (keys: C) -\u003e [Key:Value]\n\t\twhere C.Iterator.Element == Key, C.IndexDistance == Int {\n\n\t\tvar result = [Key:Value](minimumCapacity: keys.count)\n\t\tfor key in keys { result[key] = self[key] }\n\t\treturn result\n\t}\n}\n\n// Prepare an environment as close to a new OS X user account as possible.\nvar cleanctx = CustomContext(main)\nlet cleanenvvars = [\"TERM_PROGRAM\", \"SHELL\", \"TERM\", \"TMPDIR\", \"Apple_PubSub_Socket_Render\", \"TERM_PROGRAM_VERSION\", \"TERM_SESSION_ID\", \"USER\", \"SSH_AUTH_SOCK\", \"__CF_USER_TEXT_ENCODING\", \"XPC_FLAGS\", \"XPC_SERVICE_NAME\", \"SHLVL\", \"HOME\", \"LOGNAME\", \"LC_CTYPE\", \"_\"]\ncleanctx.env = cleanctx.env.filterToDictionary(keys: cleanenvvars)\ncleanctx.env[\"PATH\"] = \"/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin\"\n\n// Create a temporary directory for testing.\ncleanctx.currentdirectory = main.tempdirectory\n```\n\n## Streams\n\nThe protocols ReadableStream and WritableStream in `Context` above can read and write text from/to commands, files or the application's own standard streams. They both have an `.encoding` property they use when encoding/decoding text.\n\nYou can use `let (input,output) = streams()` to create a new pair of streams. What you write to `input` you can read from `output`.\n\n#### WritableStream\n\nWhen writing to a WritableStream you normally use `.print` which works exactly like Swift's built-in print function:\n\n```swift\nmain.stdout.print(\"everything is fine\")\nmain.stderror.print(\"no wait, something went wrong ...\")\n\nlet writefile = try open(forWriting: path) // WritableStream\nwritefile.print(\"1\", 2, 3/5, separator: \"+\", terminator: \"=\")\n```\n\nIf you want to be taken literally, use `.write` instead. It doesn't add a newline and writes exactly and only what you write:\n\n```swift\nwritefile.write(\"Read my lips:\")\n```\n\nYou can close the stream, so anyone who tries to read from the other end won't have to wait around forever:\n\n```swift\nwritefile.close()\n```\n\n#### ReadableStream\n\nWhen reading from a ReadableStream you can read everything at once:\n\n```swift\nlet readfile = try open(path) // ReadableStream\nlet contents = readfile.read()\n```\n\nThis will read everything and wait for the stream to be closed if it isn't already.\n\nYou can also read it asynchronously, that is read whatever is in there now and continue without waiting for it to be closed:\n\n```swift\nwhile let text = main.stdin.readSome() {\n\t// do something with ‘text’...\n}\n```\n\n`.readSome()` returns `String?` - if there is anything there it returns it, if the stream is closed it returns nil, and if there is nothing there and the stream is still open it will wait until either there is more content or the stream is closed.\n\nAnother way to read asynchronously is to use the `lines` method which creates a lazy sequence of Strings, one for each line in the stream:\n\n```swift\nfor line in main.stdin.lines() {\n\t// ...\n}\n```\n\nOr instead of stopping and waiting for any output you can be notified whenever there is something in the stream:\n\n```swift\nmain.stdin.onOutput { stream in\n\t// ‘stream’ refers to main.stdin\n}\n```\n\n#### Data\n\nIn addition to text, streams can also handle raw Data:\n\n```swift\nlet data = Data(...)\nwriter.write(data: data)\nreader.readSomeData()\nreader.readData() \n```\n\n## Commands\n\nAll Contexts (`CustomContext` and `main`) implement `CommandRunning`, which means they can run commands using themselves as the Context. ReadableStream and String can also run commands, they use `main` as the Context and themselves as `.stdin`. As a shortcut you can just use `run(...)` instead of `main.run(...)`\n\nThere are 4 different ways of running a command:\n\n#### Run\n\nThe simplest is to just run the command, wait until it's finished and return the results:\n\n```swift\nlet result1 = run(\"/usr/bin/executable\", \"argument1\", \"argument2\")\nlet result2 = run(\"executable\", \"argument1\", \"argument2\")\n```\n\nIf you don't provide the full path to the executable, then SwiftShell will try to find it in any of the directories in the `PATH` environment variable.\n\n`run` returns the following information:\n\n```swift\n/// Output from a `run` command.\npublic final class RunOutput {\n\n\t/// The error from running the command, if any.\n\tlet error: CommandError?\n\n\t/// Standard output, trimmed for whitespace and newline if it is single-line.\n\tlet stdout: String\n\n\t/// Standard error, trimmed for whitespace and newline if it is single-line.\n\tlet stderror: String\n\n\t/// The exit code of the command. Anything but 0 means there was an error.\n\tlet exitcode: Int\n\n\t/// Checks if the exit code is 0.\n\tlet succeeded: Bool\n}\n```\n\nFor example:\n\n```swift\nlet date = run(\"date\", \"-u\").stdout\nprint(\"Today's date in UTC is \" + date)\n```\n\n#### Print output\n\n```swift\ntry runAndPrint(\"executable\", \"arg\") \n```\n\nThis runs a command like in the terminal, where any output goes to the Context's (`main` in this case) `.stdout` and `.stderror` respectively.  If the executable could not be found, was inaccessible or not executable, or the command returned with an exit code other than zero, then `runAndPrint` will throw a `CommandError`.\n\nThe name may seem a bit cumbersome, but it explains exactly what it does. SwiftShell never prints anything without explicitly being told to.\n\n#### Asynchronous\n\n```swift\nlet command = runAsync(\"cmd\", \"-n\", 245).onCompletion { command in\n\t// be notified when the command is finished.\n}\ncommand.stdout.onOutput { stdout in \n\t// be notified when the command produces output (only on macOS).\t\n}\n\n// do something with ‘command’ while it is still running.\n\ntry command.finish() // wait for it to finish.\n```\n\n`runAsync` launches a command and continues before it's finished. It returns `AsyncCommand` which contains this:\n\n```swift\n    public let stdout: ReadableStream\n    public let stderror: ReadableStream\n\n    /// Is the command still running?\n    public var isRunning: Bool { get }\n\n    /// Terminates the command by sending the SIGTERM signal.\n    public func stop()\n\n    /// Interrupts the command by sending the SIGINT signal.\n    public func interrupt()\n\n    /// Temporarily suspends a command. Call resume() to resume a suspended command.\n    public func suspend() -\u003e Bool\n\n    /// Resumes a command previously suspended with suspend().\n    public func resume() -\u003e Bool\n\n    /// Waits for this command to finish.\n    public func finish() throws -\u003e Self\n\n    /// Waits for command to finish, then returns with exit code.\n    public func exitcode() -\u003e Int\n\n    /// Waits for the command to finish, then returns why the command terminated.\n    /// - returns: `.exited` if the command exited normally, otherwise `.uncaughtSignal`.\n    public func terminationReason() -\u003e Process.TerminationReason\n\n    /// Takes a closure to be called when the command has finished.\n    public func onCompletion(_ handler: @escaping (AsyncCommand) -\u003e Void) -\u003e Self\n```\n\nYou can process standard output and standard error, and optionally wait until it's finished and handle any errors.\n\nIf you read all of command.stderror or command.stdout it will automatically wait for the command to close its streams (and presumably finish running). You can still call `finish()` to check for errors.\n\n`runAsyncAndPrint` does the same as `runAsync`, but prints any output directly and it's return type `PrintedAsyncCommand` doesn't have the `.stdout` and `.stderror` properties.\n\n#### Parameters\n\nThe `run`* functions above take 2 different types of parameters:\n\n##### (_ executable: String, _ args: Any ...)\n\nIf the path to the executable is without any `/`, SwiftShell will try to find the full path using the `which` shell command, which searches the directories in the `PATH` environment variable in order.\n\nThe 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.\n\n```swift\ntry runAndPrint(\"echo\", \"We are\", 4, \"arguments\")\n// echo \"We are\" 4 arguments\n\nlet array = [\"But\", \"we\", \"are\"]\ntry runAndPrint(\"echo\", array, array.count + 2, \"arguments\")\n// echo But we are 5 arguments\n```\n\n##### (bash bashcommand: String)\n\nThese are the commands you normally use in the Terminal. You can use pipes and redirection and all that good stuff:\n\n```swift\ntry runAndPrint(bash: \"cmd1 arg1 | cmd2 \u003e output.txt\")\n```\n\nNote that you can achieve the same thing in pure SwiftShell, though nowhere near as succinctly:\n\n```swift\nvar file = try open(forWriting: \"output.txt\")\nrunAsync(\"cmd1\", \"arg1\").stdout.runAsync(\"cmd2\").stdout.write(to: \u0026file)\n```\n\n#### Errors\n\nIf the command provided to `runAsync` could not be launched for any reason the program will print the error to standard error and exit, as is usual in scripts. The `runAsync(\"cmd\").finish()` method throws an error if the exit code of the command is anything but 0:\n\n```swift\nlet someCommand = runAsync(\"cmd\", \"-n\", 245)\n// ...\ndo {\n\ttry someCommand.finish()\n} catch let CommandError.returnedErrorCode(command, errorcode) {\n\tprint(\"Command '\\(command)' finished with exit code \\(errorcode).\")\n}\n```\n\nThe `runAndPrint` command can also throw this error, in addition to this one if the command could not be launched:\n\n```swift\n} catch CommandError.inAccessibleExecutable(let path) {\n\t// ‘path’ is the full path to the executable\n}\n```\n\nInstead of dealing with the values from these errors you can just print them:\n\n```swift\n} catch {\n\tprint(error)\n}\n```\n\n... or if they are sufficiently serious you can print them to standard error and exit:\n\n```swift\n} catch {\n\texit(error)\n}\n```\n\nWhen at the top code level you don't need to catch any errors, but you still have to use `try`.\n\n## Setup\n\n### Stand-alone project\n\nIf you put [Misc/swiftshell-init](https://raw.githubusercontent.com/kareman/SwiftShell/master/Misc/swiftshell-init) somewhere in your $PATH you can create a new project with `swiftshell-init \u003cname\u003e`. This creates a new folder, initialises a Swift Package Manager executable folder structure, downloads the latest version of SwiftShell, creates an Xcode project and opens it. After running `swift build` you can find the compiled executable at `.build/debug/\u003cname\u003e`.\n\n### Script file using [Marathon](https://github.com/JohnSundell/Marathon)\n\nFirst add SwiftShell to Marathon: \n\n```bash\nmarathon add https://github.com/kareman/SwiftShell.git\n```\n\nThen run your Swift scripts with `marathon run \u003cname\u003e.swift`. Or add `#!/usr/bin/env marathon run` to the top of every script file and run them with `./\u003cname\u003e.swift`.\n\n### [Swift Package Manager](https://github.com/apple/swift-package-manager)\n\nAdd `.package(url: \"https://github.com/kareman/SwiftShell\", from: \"5.1.0\")` to your Package.swift:\n\n```swift\n// swift-tools-version:5.0\n// The swift-tools-version declares the minimum version of Swift required to build this package.\n\nimport PackageDescription\n\nlet package = Package(\n    name: \"ProjectName\",\n    platforms: [.macOS(.v10_13)],\n    dependencies: [\n        // Dependencies declare other packages that this package depends on.\n        .package(url: \"https://github.com/kareman/SwiftShell\", from: \"5.1.0\")\n    ],\n    targets: [\n        // Targets are the basic building blocks of a package. A target can define a module or a test suite.\n        // Targets can depend on other targets in this package, and on products in packages which this package depends on.\n        .target(\n            name: \"ProjectName\",\n            dependencies: [\"SwiftShell\"]),\n    ]\n)\n\n```\n\nand run `swift build`.\n\n### [Carthage](https://github.com/Carthage/Carthage)\n\nAdd `github \"kareman/SwiftShell\" \u003e= 5.1` to your Cartfile, then run `carthage update` and add the resulting framework to the \"Embedded Binaries\" section of the application. See [Carthage's README][carthage-installation] for further instructions.\n\n[carthage-installation]: https://github.com/Carthage/Carthage#adding-frameworks-to-an-application\n\n### [CocoaPods](https://cocoapods.org/)\n\nAdd `SwiftShell` to your `Podfile`.\n\n```Ruby\npod 'SwiftShell', '\u003e= 5.1.0'\n```\n\nThen run `pod install` to install it.\n\n## License\n\nReleased under the MIT License (MIT), https://opensource.org/licenses/MIT\n\nKåre Morstøl, [NotTooBad Software](https://nottoobadsoftware.com)\n","funding_links":[],"categories":["Command Line","Libs","Swift","Command Line [🔝](#readme)","swift-library","The Index"],"sub_categories":["Linter","Command Line","Command Line UI tools"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkareman%2FSwiftShell","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkareman%2FSwiftShell","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkareman%2FSwiftShell/lists"}