CodeNewbie Community 🌱

Cover image for Bash functions to level up your aliases game!
Imad
Imad

Posted on • Originally published at bemoore.hashnode.dev

Bash functions to level up your aliases game!

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

....

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

bash functions enable you to concatenate multiple commands and add as many 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!

Imad

Top comments (0)