(First post, be gentle.)
In a bash script, I am using:
#!/bin/bash
baseDir=$(dirname "$BASH_SOURCE")
I have saved the script as /path/to/location/script.command
, so that a double-click in Finder in macOS will execute the script.
I have created symlinks to that script from multiple other locations, but no matter the location, $baseDir
always returns as /path/to/location
.
However if I save the script as /path/to/location/script.sh
and create symlinks to that in, for example, /path/for/symlink
, then $baseDir
returns as my desired outcome, /path/for/symlink
.
My assumption is that symlinking a .sh
will always run said script from the location of the symlink, whereas symlinking a .command
will always run said script from the location of the linked script.
Unfortunately, this solution won't work because I need to resolve symlinks, and this solution won't work because $0
returns the same as $BASH_SOURCE
.
By recommendation of Charles below, I've scoured this FAQ, but unless I'm missing something, all the suggestions would require one of $0
, $BASH_SOURCE
, or PWD
to return the path to the symlink, not the path to the linked script.
Is there any way around this?