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.

link|improve this question
Just a note - FYI: . and .. are directory names. . == current directory .. == parent directory. Using them as aliases might cause a script somewhere heartburn. – jim mcnamara Nov 9 '11 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. – user1036459 Nov 11 '11 at 22:12
feedback

migrated from stackoverflow.com Nov 9 '11 at 13:31

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 8 down vote accepted

Aliases have no parameters, try function instead

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

Your Answer

 
or
required, but never shown

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