Last active
June 13, 2023 19:11
-
-
Save backslash-f/02a25d10cbb8d8ff9038bf50728da162 to your computer and use it in GitHub Desktop.
Invoke Zsh commands via a Swift script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/swift | |
import Foundation | |
func shell(_ command: String) { | |
let task = Process() | |
task.launchPath = "/bin/zsh" | |
task.arguments = ["-c", command] | |
let pipe = Pipe() | |
task.standardOutput = pipe | |
task.launch() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
let output = String(data: data, encoding: .utf8) ?? "" | |
print(output) | |
} | |
shell("export MY_VAR=123 ; echo $MY_VAR") | |
// In CLI: | |
// chmod a+x Shell.swift | |
// ./Shell.swift |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment