This really shouldn’t have taken me as long as it did to figure this out, but it was a big enough of an annoyance that I decided to share it.

How to convert a relative path to an absolute path in linux/unix/bash script:

#!/bin/bash
# Assume parameter passed in is an absolute path to a directory.
# For brevity, we won't do argument type or length checking.
echo "Absolute path: `cd $1; pwd`"


For those unaware, you don’t have to ‘cd -’ because the backticks perform command substitution and don’t modify the working directory of the script.

I like to think of snippets like this as an ingredient when cooking with technology. Individual ingredients may be good, but combining them with a recipe yields something far better :)

Leave a Reply

You must be logged in to post a comment.