DEV Community

Aaroh Mankad
Aaroh Mankad

Posted on • Updated on • Originally published at Medium

Bash Functions: A more powerful Alias

If you use bash a fair amount, you probably have some aliases set up. However, have there been times you wish you had some more control over the functionality of your alias?

Let’s examine one example: a bash alias and a bash function for extracting a file.

With an alias, we can easily abstract out the complicated tar command to simply extract.

Now we can type extract file.tar.gz or extract file.tgz and the command will extract the file. We can even specify output directories!

But what if we can’t rely on our downloaded file to have .tar.gz / .tgz extensions? Well in that case, a function for extract makes more sense!

Now we can call extract without worrying about our file extension! We can just trust that the function will do exactly what it’s meant to.

So how can I start using bash functions?

First, import a file called .bash_functions in your .bashrc.

Now you can either download my .zsh_fns file (bash functions for zsh) as a starting point, or create your own at ~/.bash_functions!

Note: You must restart terminal or type source .bashrc in your terminal to start using the new bash functions

Top comments (1)

Collapse
 
voyeg3r profile image
Sérgio Araújo • Edited

I have a function to create and go to a new dir

mkcd () {
    mkdir -pv -p "$@" && cd $_
}

Sometimes I want to make a git commit a little bit fast

gsend () {
    git commit -am "$1" && git push
}