views:

210

answers:

5

while executing a shell script in Unix Bash Shell, say any file in another folder and that is referenced by the script references some other file like ../../file_system_1/public/dir1/dir2/file2.xml

script.sh --> references dir1/file1 as "./dir1/file1" dir1/file1 references another file as ../../file_system_1/public/dir1/dir2/file2.xml

so relative to which file is the file2 resolved? to the script.sh location or the file1 folder location.

kind regards

+2  A: 

In a shell script, .. pathnames are always relative to the current working directory, not to any other file pathname. this is the value of $PWD.

bmargulies
A: 

What do you see, if you put an ls in your script.sh? That's the starting point for evaluating relative paths. Usually it's your current working directory. So to answer your question: It's probably neither script.sh's nor file1's location.

Why probably? Because it depends on what you do in your script. If you execute an cd /home/ in script.sh, the include path changes.

Boldewyn
thx boldewyn, that was it.very much appreciated.
yli
You could do `pwd` instead of `ls`. The result is more concise and usable.
Dennis Williamson
True. I just wanted to give an easily comprehensable example.
Boldewyn
+4  A: 

Relative paths are resolved in reference to the current working directory, as given by $(pwd).

Each time a resolution takes place, $(pwd) is evaluated. This means that the same ../myfile path string represents a different file before and after a cd command in your script.

mouviciel
+1  A: 

It's resolved relative to the current working directory. If the script doesn't change directories with cd or similar this is the directory in which you were when you started the script.

sth
+2  A: 

It depends on what you mean by "reference".

Relative paths are -- when used to access a file or directory -- interpreted relative to the current working directory of the process. The CWD is inherited across fork() and friends, so any sub-processes that your script executes will inherit the CWD. Interpreting relative paths is done by the C library and/or file system layer and this works the same in all programs that use the standard APIs, not just bash.

If your paths are not being used to access a file or folder, then they are just strings and do not "resolve" to anything.

thsutton