Skip to content

A portable, pure shell implementation of realpath

License

Notifications You must be signed in to change notification settings

XSven/sh-realpath

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sh-realpath

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)

Usage

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

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
/tmp/somedir/targetdir/targetpath

API

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

About

A portable, pure shell implementation of realpath

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 98.6%
  • Makefile 1.4%