Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I create BASH alias for:

I type in cdd directory and what that does is cd directory and then ls?

share|improve this question

migrated from May 20 '11 at 20:46

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

A note (though others have mentioned) aliases can not have arguments. You need a function. – Rich Homolka May 20 '11 at 23:17
Why don't you use ls directory instead? – user unknown Jan 13 '12 at 7:20

It'd be easier to make a function:

cdd () 
    cd $1

Of course, you can name the function whatever you like. Put it in your .bashrc or .profile or whatever it is on your system.

share|improve this answer
does that automatically mkae cdls an alias? – funk-shun May 19 '11 at 18:48
@funk-shun it makes cdd a function, which in practice acts almost exactly like an alias would. Functions are more powerful and take arguments, though. – Rafe Kettler May 19 '11 at 18:49
so i take it $2 refers to the second argument in a command call? -r in the case of ls -C -r? – funk-shun May 19 '11 at 18:53
@funk-shun yes. – Rafe Kettler May 19 '11 at 18:54

You want to use a function that you'll put in your .bashrc (or .bash_profile, or whatever):

  cd ${to}

Once you put this in your appropriate file, you can use cdd <directory> just like an alias.

share|improve this answer
This will break on directories with spaces in them – Daenyth May 19 '11 at 19:55

Just like the other function examples, but this one will work with directories with spaces, without needing to escape the spaces.

cdd() {
    cd "$*"
share|improve this answer
alias dirXandLs='cd directory; ls'

I bet you really want to make directory be an argument, i.e. $1. can't do that with aliases.

I hope that helps.

share|improve this answer

I think here is your answer. you can add this function to your .bashrc file:

function cd(){ builtin cd "$*" && ls }
share|improve this answer

You must log in to answer this question.

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