Matt Andrews
Matt Andrews

Reputation: 2878

How can I add a bash alias to .bashrc which will accept an argument?

I'm new to Linux but want to experiment with aliases.

I have a directory of branches of our software release. The directory structure looks like this:

/home/username/software/release-1
/home/username/software/release-2
/home/username/software/release-3 etc

I'd like to add an alias so I could type something like "cdr 1" and automatically cd into the release-1 directory.

I know I could add these lines to my .bashrc:

alias cdr 1='cs /home/username/software/release-1'
alias cdr 2='cs /home/username/software/release-2' etc

but is there some way to pass an argument into the alias so I don't have to update it for every new release?

thanks Matt

Upvotes: 2

Views: 1459

Answers (1)

bash-o-logist
bash-o-logist

Reputation: 6911

you can put them in a function

cdr(){
  to=$1
  cd /home/username/software/release-${to}
}

to use,

...
cdr 1
cdr 2
...

Upvotes: 12

Related Questions