I would like to make a bash script that accepts both relative and absolute paths. It would be easy to filter with the first character, but I wonder if there is a more elegant way.
2 Answers
You could do a conversion to absolute path for everything passed in using readlink?
e.g. say you're in /home/foo, which contains a folder 'bar':
readlink -f bar -> /home/foo/bar
readlink -f /home/foo/bar -> /home/foo/bar
1 Comment
Labo
Thank you a lot, I did not knew that usage of readlink (neither realpath) !!! (I upvoted you answer but have not enough reputation for it to become visible)
[ -f "$var" ]) If you are talking about positional parameters, then you can test existence and then usereadlink -forrealpathas suggested by jam. If however, you are not in a context where the test for existence will be true, then neither will help.