A portable, pure shell implementation of realpath
Shell Makefile
Switch branches/tags
Nothing to show
Clone or download
Latest commit 6551236 Dec 27, 2017



A portable, pure shell implementation of realpath

Copy the functions in realpath.sh into your shell script to avoid introducing a dependency on either realpath or readlink -f, since:

  • realpath does not come installed by default
  • readlink -f is not portable to OS-X (relevant man page)


$ source ./realpath.sh
$ realpath /proc/self

Or we can get tricky:

$ cd /tmp
$ mkdir -p somedir/targetdir somedir/anotherdir
$ ln -s somedir somedirlink
$ ln -s somedir/anotherdir/../anotherlink somelink
$ ln -s targetdir/targetpath somedir/anotherlink
$ realpath .///somedirlink/././anotherdir/../../somelink


Note: unlike realpath(1), these functions take no options; do not use -- to escape any arguments

Function Description
realpath PATH
Resolve all symlinks to PATH, then output the canonicalized result
resolve_symlinks PATH
If PATH is a symlink, follow it as many times as possible; output the path of the first non-symlink found
canonicalize_path PATH
Output absolute path that PATH refers to, resolving any relative directories (., ..) in PATH and any symlinks in PATH's ancestor directories

readlink Emulation

realpath.sh includes optional readlink emulation. It exposes a readlink function that calls the system readlink(1) if it exists. Otherwise it uses stat(1) to emulate the same functionality. In contrast to the functions in the previous section, you may pass -- as the first argument, since you may be calling the system readlink(1).