0

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.

1
  • Your question is a bit vague. How, when, where do you want to take input that may be relative or absolute? The reason being, it matters whether you can test for file existence. (e.g. [ -f "$var" ]) If you are talking about positional parameters, then you can test existence and then use readlink -f or realpath as suggested by jam. If however, you are not in a context where the test for existence will be true, then neither will help. Commented Oct 12, 2015 at 8:44

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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)
0

Using readlink -f or realpath fits perfectly my needs !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.