NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How do you get current directory for command line tools?

Forums > macOS

For fun, I'm writing a Swift command line tool for macOS. I want to pass in an argument like "foo.txt", which would be a text file in the current directory. I'm unclear on how the app will know what directory the shell is currently in. How do you pass the current directory to the command line apps implicity so the user does not have to specify them?


I think FileManager.currentDirectoryPath is what you are looking for.


Thank you, David. I tested it and you are correct!

I disregarded currentDirectoryPath initially because the documentation says: "The path to the program’s current directory."

If I'd read the full docs, I'd have seen the warning: "This property reports the current working directory for the current process, not just the receiver."

Which is exactly what I wanted, thanks again!


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.