2

I'm adding a couple of aliases to my .bashrc file for some shortcuts I use often, among which are:

alias .="cd /;ls"
alias ..="cd ../;ls"
alias cd="cd $1;ls"
alias ls="clear;pwd;ls"

The effective result being that whenever I navigate to a new directory, the window is cleared and displays the new directory and its contents at the top.

All of these aliases work except for the cd alias. I have tried multiple combinations of trying to cd in to the new directory and run ls there, but it seems like I can only either list the contents of the directory and return to the old directory, or successfully cd in to the directory but with the generated list being from the old directory.

Is there something special that needs to be done when aliasing cd (or any command with arguments) in bash? I feel like this is a relatively simple task but I'm just overlooking something with the syntax.

2
  • 1
    Just a note - FYI: . and .. are directory names. . == current directory .. == parent directory. Using them as aliases might cause a script somewhere heartburn. Nov 9, 2011 at 21:32
  • Duly noted. I'm just tinkering with bash and C++ on a spare install right now, trying to get my bearings. So I could run sudo rm -rf / and be fine :) but I'll keep that in mind for later scripts.
    – Zee
    Nov 11, 2011 at 22:12

1 Answer 1

10

Aliases have no parameters, try function instead

cd() { builtin cd "$1" ; ls ; }
1
  • Thanks man, worked like a charm. Never would've guessed that'd do it either. TIL how to create functions in bash, haha
    – Zee
    Nov 11, 2011 at 22:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .