Skip to content

Instantly share code, notes, and snippets.

@meoow
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoow/0c33aa47ecead69b0b42 to your computer and use it in GitHub Desktop.
Save meoow/0c33aa47ecead69b0b42 to your computer and use it in GitHub Desktop.
return absolute path in pure bash
#get absolute path(shell)
abspath(){ #{{{
local thePath
if [[ ! "$1" =~ ^/ ]];then
thePath="$PWD/$1"
else
thePath="$1"
fi
IFS=/ parr=($thePath)
declare -a outp
for i in "${parr[@]}";do
case "$i" in
''|.) continue ;;
..)
len=${#outp[@]}
if ((len==0));then
continue
else
unset outp[len-1]
fi
;;
*)
len=${#outp[@]}
outp[len]=$i
;;
esac
done
echo /"${outp[*]}"
} #}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment