12

How can I call an external command (launch a subprocess) from a Swift script?

Perhaps something like call(["ls", "-l"]) in Python.

1 Answer 1

20

You can still use NSTask in Swift. Your example would be something like this.

let task = NSTask() task.launchPath = "/bin/ls" task.arguments = ["-l"] task.launch() 

Swift 3+, macOS 10.13+

let task = Process() task.executableURL = URL(fileURLWithPath: "/bin/ls") task.arguments = ["-l"] task.run() 
Sign up to request clarification or add additional context in comments.

2 Comments

Ah yeah. duh.. Still wrapping my head around this thing :)
This is outdated: use Process with Swift 3.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.