Having a hard time understanding why readlink seems to be dependent from where it is called from rather than supplying an actual path when used with custom scripts.
Environment
WSL Ubuntu 20 on Windows 11
Steps to reproduce
- Navigate to a folder listed in
PATH, e.g./usr/local/bin - Create a new file
linktestwhich contains this code:
#!/bin/bash
echo $(readlink -f $1)
- Save it and give it executable permissions via
sudo chmod +x linktest - Create an arbitrary file like
demo.txtin the same folder - Call
linktest demo.txt, output will be/usr/local/bin/demo.txt - Navigate to any other folder like
/home/<user> - Call
linktest demo.txtagain, output will be/home/<user>/demo.txtinstead of/usr/local/bin/demo.txt
Apart from the fact that /home/<user>/demo.txt does not actually exist, I wonder why readlink behaves relative instead of absolute. How can I get the actual, absolute path independent from the origin of the call?