Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • Well, if we don't care abount symbolic links, then a simple dirname $0 will do. But I need to care, and in that case, your code will certainly fail. The which command cannot be used, please read the question again. These scripts must be location independent, and they may not be on PATH at all. (Only some symlinks to some of them)
    – nagylzs
    Commented Oct 22, 2018 at 11:13
  • I guess my first answer was ambiguous, I edited it to give you complete explanation, with a working sample. Hoping it reaches your needs. Commented Oct 22, 2018 at 11:36
  • See How to check if a program exists from a Bash script? for information about problems with which, and what to use instead.
    – pjh
    Commented Oct 22, 2018 at 19:20
  • For applications like this, ${BASH_SOURCE[0]} is better than $0. See choosing between $0 and BASH_SOURCE.
    – pjh
    Commented Oct 22, 2018 at 19:29