1

I had given an answer on SO, the question was concerning a problem with a relative path. My answer was to use an absolute path, which I thought seemed simpler. The asker claimed to need a relative path, but I couldn't tell why.

It seems reasonable that if you know to go to ../../../foo/bar/, then you would also know to go to /home/willie/foo/bar/.

I can't think of a situation which requires relative paths except in a build script. Are there others I'm not thinking of? Is it bad practice to use one or the other in any situation?

Sorry if this question is off topic, all my googling yielded was how to use them rather than why.

  • 7
    Any time that you don't know ahead of time where the root folder will be residing. There are many such scenarios besides build scripts. – Robert Harvey Aug 21 '15 at 22:47
2

I can't think of a situation which requires relative paths

A possible reason to use relative path would be to use paths related to the location of the executable (and not to the current work directory); e.g. if your executable sits in /home/will/bin/foo you would use /home/will/bin/../data/resource (which is /home/will/data/resource) for some resource files. See this answer for details.

Then, you could ship a file tree (e.g. a .tar archive) containing the executable (with some relative paths like bin/foo for the executable and data/resource for resources used by it), your user would install it at any place he likes (e.g. under /home/will/, or /home/joe/soft/ or /opt/ ...) and be able to run the executables and access resources.

This practice is not very common (IIRC, xemacs used it), because most systems have conventions about files, like the Filesystem Hierarchy Standard & hier(7) and software gets installed (e.g. by some make install step) there.

  • 1
    The practice is still common. Any time you use hardcoded paths you can't place the file where it suits you. There are many legit reasons the (many different) conventions won't always suit everyone. – whatsisname Aug 22 '15 at 2:22
  • Even with filesystem standards, there are still multiple choices for base installation location, e.g. /usr, /usr/local, /opt/... – Jules Aug 22 '15 at 8:41
  • @Jules: but for a given software (and configured build tree), it is quite likely that it should be installed at some well defined (and single) place. – Basile Starynkevitch Aug 22 '15 at 15:22
  • In an ideal world, the choice of which location to install in should be left to the local administrator. While this can be done (at least for open source software) by build-time configuration, I believe using paths relative to the binary location is generally a more friendly solution, and is probably the only completely flexible solution for non-oss applications. – Jules Aug 23 '15 at 6:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.