-2

If a bash script contains a relative path ( to specify a file, a directory, ...), which path is the relative path relative to,

  • the pathname to the parent directory of the bash script, or
  • the current directory where we run the bash script?

If I may ask, do similar questions in a Python script, in a Java program, in a C program, in a C++ program, .... have the same answer?

Thanks.

I don't see any similarity with the linked question. I am asking about what a relative path in a program is relative to.

Tim
  • 101,790
  • Isn't that extremely simple to find out? – Hauke Laging Mar 18 '18 at 02:08
  • whatever the answer is, why is it? – Tim Mar 18 '18 at 02:09
  • I don't see any similarity with the linked question. I am asking about what a relative path in a program is relative to here. – Tim Mar 20 '18 at 18:50
  • @Tim The similarity is both your question and the linked question have the same answer. The answer to your question "which path is the relative path relative to" is whatever directory that pwd outputs, i.e. the current "working directory". – jw013 Mar 20 '18 at 19:32

1 Answers1

2

The parent directory is the directory that includes an item:

  • For a file, the path to its parent is ., because that's where the file is placed. The file itself is ./file.
  • For a directory, the parent is .. and . is the directory itself.

This is a general notation for Linux, and is widely met. Theoretically some languages might use a different notation, but I can't give an example.

Here's an illustration using absolute paths, from which you can easily derive the relative paths with the power of your brain.

$ echo 'readlink -f "$0"' > script
$ ln -s script link
$ chmod +x script
$ ./script 
/home/tomas/x/script
$ ./link 
/home/tomas/x/script
$ readlink -f .
/home/tomas/x
$ readlink -f ..
/home/tomas