Execute a shell command


I want to execute a shell command (as in the terminal) and get its output.


// Create a Task instance
let task = NSTask()

// Set the task parameters
task.launchPath = "/usr/bin/env"
task.arguments = ["pwd"]
// Create a Pipe and make the task
// put all the output there
let pipe = NSPipe()
task.standardOutput = pipe

// Launch the task
// Get the data
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: data, encoding: NSUTF8StringEncoding)


Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)