DISQUS

blogt0sk1: cd up, up, and a-up

  • someguy · 3 months ago
    This seems unnecessarily complicated. It's also not safe for pathnames with spaces or other shellcandy. Try:

    function cd () {
    if [ ${1:0:2} == '..' ]; then
    rest=${1:2}
    rest=${rest//./../}
    builtin cd "${1:0:2}/${rest}"
    else
    builtin cd "$1";
    fi
    }
  • Jerod Santo · 3 months ago
    You're absolutely right, my function doesn't handle spaces in pathnames at all. I'll give yours a try and if it works as advertised I'll be using it instead, thanks!

    I'm not familiar with:

    ${1:0:2}

    What does that mean?
  • someguy · 3 months ago
    ${foo:x:y} == foo[x:y] in Python.
  • Jerod Santo · 3 months ago
    So you're accessing a subset of $1. That makes sense.

    Your function works great in every situation except when you don't pass any args to cd at all. In this case, it fails. I added a check for that case and updated the function in the post. Let me know if it can be further improved.

    Thanks!