Sample Header Ad - 728x90

Symbolic link to application - working directory

0 votes
1 answer
283 views
Lets say i have a bash script in a folder called
, which reads data from files in a subfolder named
.
The paths in the script to the files are all relative to this "Programming" folder.
"test" > ./data/test.txt
.
So the working directory should always be the "Programming" folder.
When the script passes all tests, it is finished and i thought i could easily copy the whole content of the "Programming" folder to a subfolder in
/opt
, lets say
/opt/myapplication
and then make a symbolic link in the
/usr/bin
folder called
, which links to the script in
/opt/myapplication/bash_script
to make it reachable in the environment. But the problem is, if i now call myapplication from anywhere the working directory is
$PWD= #actual shell directory at script start
$0=/usr/bin/myapplication
${BASH_SOURCE}=/usr/bin/myapplication
and my application fails. I thought the symbolic link will change the working directory, to where the file is located. What is the common practice in Unix to set the working directory always, to where the executed file is located, to use relative paths ??
Asked by Schmaehgrunza (370 rep)
Feb 17, 2024, 06:21 PM
Last activity: Feb 18, 2024, 04:49 PM