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.

5
  • 2
    Most of the code is only there to resolve symlinks. Do you really need that? I think dirname "$0" should be sufficient in your case.
    – Socowi
    Commented Oct 22, 2018 at 9:10
  • These small scripts will be symlinked to ~/bin directories of various users on the system. So yes, reading symlinks is important.
    – nagylzs
    Commented Oct 22, 2018 at 9:24
  • 2
    I'm fairly sure you won't get better result than the question you linked. If you need to reuse the code, you could declare it in a system wide function or alias, or even just an external script you systematically call before your main script execution
    – Aserre
    Commented Oct 22, 2018 at 9:49
  • 2
    Typically, you avoid this by putting your code in well-known places when you install it, rather than making the script figure out what random directory you placed it in.
    – chepner
    Commented Oct 22, 2018 at 12:10
  • You are all saying that creating location independent code is a bad idea. The location of the script is the environment, and the script can use it. It just happens to be clumsy in bash. It proves that bash is not the perfect tool for the task. But it does not prove that location independent code is bad. Is there a good reason why not to write location independent code? I don't understand why "relative paths should be avoided"? Maybe they should be, but I need an explanation.
    – nagylzs
    Commented Oct 22, 2018 at 15:38