I have a Terminal shell script file start.command that I launch from finder with:
ls -l
The file is in ~/foo but list ~, can I get the path of it’s containing dir. I’d like to launch an application that is in the same folder as the file when the user runs the .command but it seems like I would need the absolute path to the file for that to work.
Assuming bash, you need to
cdto the script’s enclosing directory before runningls.You can one-line it with this:
$0is the script’s relative path, i.e. whatever you used on the command line to invoke it.dirnamestrips the filename from the path.$()is a value expansion to feed tocd, and the quotes are all necessary to handle things like spaces in paths.