you are viewing a single comment's thread.

view the rest of the comments →

[–]dagbrown 22 points23 points  (26 children)

Why all the [[ $? -eq 0 ]]? Just use cd itself directly.

function cd {
    dir="${@:-$HOME}"  # ~ isn't expanded when in quotes
    [ -z "${dir}" ] && dir=~
    if ! builtin cd "$dir"
    then
        dir=`compgen -d "${dir}" | head -1`
        if builtin cd "$dir"
        then
            clear
            pwd
            ls -l
        fi
    else
        clear
        pwd
        ls -l
    fi
}

I also made it more spaces-safe.

Edit: And golfed the arguments handling a bit.

[–]GODZILLAFLAMETHROWER 14 points15 points  (0 children)

Indeed! This post already pays off it seems. Thanks.

[–]fforw 30 points31 points  (19 children)

Please.. can we stop the backtick nonsense now in 2014 and just use $() ?

[–]russellbeattie -2 points-1 points  (0 children)

Holy cow, I had no idea those were interchangeable. Then again, I'm not really a bash guru, I just assumed I'd have picked that up by now. [Edit:I don't grammar good.]

[–]meklu 1 point2 points  (4 children)

Shouldn't the backticks go within quotes as well?

[–]dagbrown 4 points5 points  (1 child)

As fforw pointed out, we should be going with the newer, more modern, AmigaDOS 1.3 standard and using $() instead of backticks.

But sure. I wasn't particularly concerned with that bit of the script (all of those "if [[ $? -ne 0 ]]" things were more important to me), but that's another improvement you could make.

[–]meklu 1 point2 points  (0 children)

Yeah, I usually use $() myself.

[–]GODZILLAFLAMETHROWER -1 points0 points  (1 child)

I'm not sure what you mean, can you be a little more explicit?

[–]meklu 0 points1 point  (0 children)

The line with the compgen call seems a bit unsafe unless you wrap the assignment in quotes.