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)



Find answers here

Hi there! Ask Us a Question We are ready to help