Created
July 8, 2011 18:00
-
-
Save jedisct1/1072377 to your computer and use it in GitHub Desktop.
realpath() in shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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