BASH : Alias and Parameters

by

I was trying to alias “cd” today to do more than just change the directory. The simple approach would be

alias cd="cd $1 && pwd > ~/pwd

or maybe

alias cd="cd \$1 && pwd > ~/pwd"

After trying the above and various other combinations, I found that bash alias does not accept parameters. So, to use parameters in alias one needs to do the following ( in your .bashrc / .bash_profile file)

mycd()
{
cd $1 && pwd > ~/pwd
}
alias cd=mycd

Adding $2 $3 etc allows you to send more parameters as required.

Special thanks to Jitesh for help regarding this find. 🙂

Advertisements

One Response to “BASH : Alias and Parameters”

  1. Amod Jaltade Says:

    Nice ! awesomely useful 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: