0

(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?

  • FYI -- `$0` is somewhat innately unsafe. Consider using `$BASH_SOURCE` instead, and use quotes: `basedir=$(dirname "$BASH_SOURCE")`, f/e. Even if you're sticking with `$0`, `basedir=$(dirname "$0")` is still less buggy (will work correctly if the script is in a location with spaces, for example). – Charles Duffy Jul 16 '18 at 20:58
  • 1
    ...using lowercase in the above comment is intentional, btw -- all-caps names are used for variables meaningful to the shell itself; using lowercase names for your own variables avoids overwriting meaningful variables by accident; see relevant standard at http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, fourth paragraph: *The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities.* – Charles Duffy Jul 16 '18 at 20:59
  • ...reading the above, keep in mind that defining a shell variable overwrites any like-named environment variable. Anyhow, in terms of what `$0` contains, that's actually up to the caller, not the script itself. If you're launching a script from Finder, it's Finder's choice whether to provide an absolute path in `$0`. If it only gives you the resolved path, then, well, the resolved path is all you have. – Charles Duffy Jul 16 '18 at 21:00
  • Getting back towards the question, https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself is *almost* what you want, except that it doesn't want symlinks resolved. This is actually a little harder on MacOS than on GNU platforms, which provide `readlink -f` – Charles Duffy Jul 16 '18 at 21:04
  • With respect to what you're looking for, though, see [BashFAQ #28](https://mywiki.wooledge.org/BashFAQ/028) – Charles Duffy Jul 16 '18 at 21:05
  • Thanks @CharlesDuffy. Good call on `$BASH_SOURCE` instead of `$0`, I was unaware of that one. I'll have a look through that FAQ and see if I can find anything that might help. – NomadGnome Jul 16 '18 at 21:13
  • @CharlesDuffy I couldn't find anything other than `$0`, `$BASH_SOURCE`, or `PWD` in the FAQ, but thanks for the link! It'll definitely be helpful in future troubleshooting. I've updated my question to fix the syntax and to clarify the difference between my question and similar previous questions. – NomadGnome Jul 16 '18 at 21:49
  • So you want the symlink, not the resolved location -- I actually interpreted this as trying to go in the other direction. That might not be possible at all, but it's not a duplicate of a preexisting question, so I'll reopen. (If I'm interpreting the question correctly now, would *How can I locate the symlink through which my script was started from Finder?* be an accurate title? I think it might be a bit more clear). – Charles Duffy Jul 16 '18 at 21:54
  • @CharlesDuffy Good idea. Updated. – NomadGnome Jul 16 '18 at 22:06

0 Answers0