Skip to content

Instantly share code, notes, and snippets.

@jedisct1
Created July 8, 2011 18:00
Show Gist options
  • Select an option

  • Save jedisct1/1072377 to your computer and use it in GitHub Desktop.

Select an option

Save jedisct1/1072377 to your computer and use it in GitHub Desktop.
realpath() in shell
realpath() {
local file="$1"
local _file
local realpath_file
while _file=$(readlink -- "$_file"); do file="$file"; done
if [ -d "$file" ]; then
realpath_file=$(cd -- "$file" && pwd -P)
else
local dir=$(dirname -- "$file")
local realpath_dir=$(cd -- "$dir" && pwd -P)
local sep="/"
[ "$realpath_dir" = "/" ] && sep=""
realpath_file="${realpath_dir}${sep}"$(basename -- "$file")
fi
[ -e "$realpath_file" ] || realpath_file="$file"
echo "$realpath_file"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment