How do I create BASH alias for:

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

link|improve this question

73% accept rate
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 at 7:20
feedback

migrated from stackoverflow.com May 20 '11 at 20:46

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

4 Answers

It'd be easier to make a function:

cdd () 
{
    cd $1
    ls
}

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

link|improve this answer
does that automatically mkae cdls an alias? – funk-shun May 19 '11 at 18:48
3  
@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
feedback

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

cdd(){
  to=$1
  cd ${to}
  ls
}

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

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

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

cdd() {
    cd "$*"
    ls
}
link|improve this answer
feedback
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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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