I am writing a script to set environment variables on linux 2.6 using bash. So the script contains commands like:
export SRC_DIR=..
export LIBPATH=${SRC_DIR}/lib
The readlink
command is capable of not only resolving symlinks, but also canonicalizing relative paths. Be careful, since you may not want the behaviour of resolving symlinks in all your scripts. If you don't want to resolve symlinks, pwd
would be the best; note the use of a subshell, so the cd command does not affect the working directory in the main shell.
# The path you want to get information on... (for readability)
your_path=..
# Using /bin/readlink (resolves symlinks)
export SRC_DIR=$(readlink --canonicalize $your_path)
# Using /usr/bin/dirname (keeps symlinks)
export SRC_DIR=$(cd $your_path ; pwd)