3
1
How do I create BASH alias for:
I type in cdd directory
and what that does is cd directory
and then ls
?
3
1
How do I create BASH alias for:
I type in cdd directory
and what that does is cd directory
and then ls
?
8
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.
does that automatically mkae cdls an alias? – funk-shun – 2011-05-19T18:48:37.307
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 – 2011-05-19T18:49:48.133
so i take it $2 refers to the second argument in a command call? -r
in the case of ls -C -r
? – funk-shun – 2011-05-19T18:53:30.813
@funk-shun yes. – Rafe Kettler – 2011-05-19T18:54:20.253
2
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.
1This will break on directories with spaces in them – Daenyth – 2011-05-19T19:55:57.813
2
Just like the other function examples, but this one will work with directories with spaces, without needing to escape the spaces.
cdd() {
cd "$*"
ls
}
1
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.
1
I think here is your answer. you can add this function to your .bashrc file:
function cd(){ builtin cd "$*" && ls }
1A note (though others have mentioned) aliases can not have arguments. You need a function. – Rich Homolka – 2011-05-20T23:17:38.920
1Why don't you use
ls directory
instead? – user unknown – 2012-01-13T07:20:22.767