Sign in
Log inSign up
Bash functions to level up your aliases game!

Bash functions to level up your aliases game!

Bemoore's photo
Bemoore
·Sep 11, 2020·

2 min read

Do you use bash aliases to customise your commands? Of course you do!

....

But, did you know you can extend aliases to use bash functions and pass arguments?

bash functions enables you to concatenate multiple commands and add as much arguments as you can. Well, just like any function in any programming language.

I came across this when i was looking into condensing git commands. I AM EXTREMELY LAZY, and running git add + git commit + git push every time, really hurts.

So let's make a function that does just that.

STEP 1

We need to make the alias persistent in bash profile. Open a new bash terminal window and run :

Alt Text

STEP 2

Anywhere in your bashrc file, declare the function as shown below.

Alt Text

STEP 3

Save and exit nano.

Here is what we did, we declared pushtoremote function, which runs git add . git commit -m message and git push repository branch consecutively

$1 $2 $3 are placeholders for the variables passed to the alias upon executing. message repository branch (example follows).

STEP 4

We need to link the alias to the current session, so we can immediately start using it.

Alt Text

STEP 5

Here is a sample command with pushtoremote alias.

Alt Text

Note : some terminals like fish do not support && so you will have to replace it with and

If you make something extra cool with bash functions, please do let me know. :)))

I post snippets every day on twitter, join me there!

Cheers!

IYO