Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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

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

5 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.

share|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
add comment

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.

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

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

cdd() {
    cd "$*"
    ls
}
share|improve this answer
add comment
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
add comment

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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