`readlink` not returning absolute path when used with script located in folder known to `PATH`

105 Views Asked by At

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

  1. Navigate to a folder listed in PATH, e.g. /usr/local/bin
  2. Create a new file linktest which contains this code:
#!/bin/bash
echo $(readlink -f $1)
  1. Save it and give it executable permissions via sudo chmod +x linktest
  2. Create an arbitrary file like demo.txt in the same folder
  3. Call linktest demo.txt, output will be /usr/local/bin/demo.txt
  4. Navigate to any other folder like /home/<user>
  5. Call linktest demo.txt again, output will be /home/<user>/demo.txt instead 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?

0

There are 0 best solutions below