Optimize _lp_shorten_path by using bash/zsh features

Optimizations:
- move variable declaration in blocks where they will be used
  (so they are not computed in blocks where they are not used)
- `local -i` for integer variables
- `if [[ $(( ... )) -gt $(( ... )) ]]`   ==>  `if (( ... > ... ))`
- `i=$((i+1))`   ==>  `let i++`

As a side effect the readability of the code is improved.