I have set up an alias in my .bashrc as follows:

alias u='cd ..'

All is well in my world... until I type cd .. and cringe that I did not use my incredible new alias. In fact, with this particular thing, it's very ingrained. Hard to change my behavior - I need serious intervention.

So, I naturally tried to set up another alias to keep me from using cd ..:

alias 'cd ..'='echo "Use your alias!"'

But that apparently doesn't work. My thought is that this also might somehow conflict with the u alias, in some sort of infinite loop of aliasing.

Any ideas?

  • OK, to note: I don't care if this uses aliases or not - My real goal is to stop myself from using cd .. somehow. Perhaps I should change the question title? – kenny May 7 '11 at 19:47
  • I used to use cd.. (no spaces) all the time in DOS. Took me a while to break that habit in Linux back in the day. – LawrenceC May 8 '11 at 3:32

Anything more complicated that supplying a few extra arguments to a command is too much for an alias and requires a function instead. Use builtin cd to call the original.

cd () {
  if [ "$*" = ".." ]; then
    echo 1>&2 'Use your alias instead!'
    return 2
    builtin cd "$@"

If you're running bash ≥4.0, I question the utility of this particular alias. Put shopt -s autocd in your ~/.bashrc, and just type .. or any other directory name to switch to it.

  • I might have to use this to break my cd habit... although... at work I wouldn't have access to zsh autocd... oh choices... – xenoterracide May 8 '11 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.