Silvering
Silvering

Reputation: 787

Swift Shell command echo in file

func exec(_ path: String, _ args: String...) -> Int32 {
        let task = Process()
            task.launchPath = path
            task.arguments = args
            task.launch()
            task.waitUntilExit()
        
        return task.terminationStatus
    }

exec("/bin/echo", "toto", ">>", "pathToFile")

Hi everyone! Someone could explain me why this code print into xcode terminal instead of write to the file ? Thanks!

Upvotes: 2

Views: 1307

Answers (1)

CRD
CRD

Reputation: 53000

Multiple commands, pipes, redirection, etc., etc. are all handled by the shell, not by individual commands themselves. If you want to run "echo" and redirect its output you must run the shell and pass it the command line to parse and execute. Try:

exec("/bin/sh", "-c", "echo toto >> /tmp/pathToFile")

Upvotes: 2

Related Questions