Skip to content

Instantly share code, notes, and snippets.

@x100ex
Last active August 29, 2015 14:05
Show Gist options
  • Save x100ex/2ed7dc3414ee42ea883a to your computer and use it in GitHub Desktop.
Save x100ex/2ed7dc3414ee42ea883a to your computer and use it in GitHub Desktop.
pure bash abspath
#!/bin/bash
abspath() {
local thePath
if [[ ! "$1" =~ ^/ ]]
then
thePath="$PWD/$1"
else
thePath="$1"
fi
echo "$thePath"|(
IFS=/
read -a parr
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