Skip to content

Instantly share code, notes, and snippets.

@theangelperalta
Created February 14, 2019 01:24
Show Gist options
  • Save theangelperalta/7b8ad82ae38f75f9533ea3b7e7a1944c to your computer and use it in GitHub Desktop.
Save theangelperalta/7b8ad82ae38f75f9533ea3b7e7a1944c to your computer and use it in GitHub Desktop.
Run Shell Commands from Swift
#!/usr/bin/env swift
import Foundation
@discardableResult
func shell(_ command: String) -> String {
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return output
}
shell("ls")
shell("open -a safari")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment