Skip to content

Instantly share code, notes, and snippets.

@mheffner
Created August 26, 2009 13:31
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 mheffner/175508 to your computer and use it in GitHub Desktop.
Save mheffner/175508 to your computer and use it in GitHub Desktop.
function traildir()
{
local n=$1 dir=$2
local sl tildedir homelen shifted traildir
local oldifs=$IFS
tildedir=${dir#$HOME}
if ! [[ "$tildedir" == "$dir" ]]; then
# Special break out case
[[ -z "$tildedir" ]] && { echo "~"; return 0; }
sl="~/"
else
sl="/"
fi
set -- ${HOME//\// }
homelen=$#
shifted=0
IFS="/"
set -- ${tildedir/\//}
if [[ $# -gt $n ]]; then
shifted=$(($# - $n))
shift $shifted
traildir="$sl<$shifted>/$*"
else
traildir="$sl$*"
fi
IFS=$oldifs
echo $traildir
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment