Friday, September 21, 2018

Make a Bash alias that takes a parameter?



I used to use CShell (), which lets you make an alias that takes a parameter. The notation was something like



alias junk="mv \\!* ~/.Trash"


In Bash, this does not seem to work. Given that Bash has a multitude of useful features, I would assume that this one has been implemented but I am wondering how.


Answer



Bash alias does not directly accept parameters. You will have to create a function.




alias does not accept parameters but a function can be called just like an alias. For example:



myfunction() {
#do things with parameters like $1 such as
mv "$1" "$1.bak"
cp "$2" "$1"
}



myfunction old.conf new.conf #calls `myfunction`


By the way, Bash functions defined in your .bashrc and other files are available as commands within your shell. So for instance you can call the earlier function like this



$ myfunction original.conf my.conf

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...