What is the best way to write a shell script that will access files relative to it such that it doesn't matter where I call it from? "Easy" means the easiest/recommended way that will work across different systems/shells.
Example
Say I have a folder ~/MyProject
with subfolders scripts/
and files/
. In scripts/
, I have a shell script foo.sh
that wants to access files in files/
:
if [ -f "../files/somefile.ext" ]; then
echo "File found"
else
echo "File not found"
fi
It'll work fine If I do cd ~/MyProject/scripts && ./foo.sh
, but it will fail with cd ~/MyProject && scripts/foo.sh
.