หากคุณต้องการใช้สภาพแวดล้อม bash สำหรับการเรียกคำสั่งให้ใช้ฟังก์ชัน bash ต่อไปนี้ซึ่งใช้ Legoless เวอร์ชันที่แก้ไขแล้ว ฉันต้องลบบรรทัดใหม่ต่อท้ายออกจากผลลัพธ์ของฟังก์ชันเชลล์
สวิฟต์ 3.0: (Xcode8)
import Foundation
func shell(launchPath: String, arguments: [String]) -> String
{
    let task = Process()
    task.launchPath = launchPath
    task.arguments = arguments
    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = String(data: data, encoding: String.Encoding.utf8)!
    if output.characters.count > 0 {
        
        let lastIndex = output.index(before: output.endIndex)
        return output[output.startIndex ..< lastIndex]
    }
    return output
}
func bash(command: String, arguments: [String]) -> String {
    let whichPathForCommand = shell(launchPath: "/bin/bash", arguments: [ "-l", "-c", "which \(command)" ])
    return shell(launchPath: whichPathForCommand, arguments: arguments)
}
ตัวอย่างเช่นเพื่อรับสาขา git การทำงานปัจจุบันของไดเร็กทอรีการทำงานปัจจุบัน:
let currentBranch = bash("git", arguments: ["describe", "--contains", "--all", "HEAD"])
print("current branch:\(currentBranch)")