I'm writing a script in Swift, and I want it to modify some files that always exist in the same directory as the script itself. Is there a way to get the path to the script from within itself? I tried:
print(Process.arguments)
But that outputs only the path that was actually given to the script, which may be the fully resolved path, just the file name, or anything in between.
I'm intending the script to be run with swift /path/to/my/script.swift
.
just in swift:
The accepted answer doesn't work in Swift 3. Also, this is a more straightforward approach:
However, it has the same problem pointed out by @rob-napier, if the script's directory is in your PATH.