DEV Community

Cover image for Working with Shell Alias and Functions in linux
salem ododa
salem ododa

Posted on • Updated on

Working with Shell Alias and Functions in linux

Alias helps to pass commands to the linux terminal to run a specific instruction.
The linux terminal is very resourceful, and as such utilizing Alias could be a major boost to your productivity as a developer.
As a "lazy" dev like myself :)) i use aliases for any command that spans beyond 3 words, don't blame me, cos why should i bother typing a long array of words when i could pass in two letters that represents the same command set.
Now you might not be a fan of shortening commands when writing codes, but i find alias quite helpful when you're trying to remember long commands, as they allow you map short, self-made commands rather than the default that comes with the compiler/interpreter or whatever interface you're working on.
For example this is an alias i use whenever i'm trying to run a unittest in my python projects:
alias test="python -m unittest"
you can see it is much more shorter and saves me the stress of typing everything out repeatedly.

Creating Alias

Creating an alias is pretty straight-forward, normally user-defined alias can be created in either the .bashrc or .bash_aliases files, i'd personally recommend using the .bashrc file for easy organization and referencing.
The syntax for adding an alias is:
sudo nano ~/.bashrc
alias name=command
to create an alias you have to indicate with the alias command.

Flags when working with alias

Flags are optional arguments passed along side a command to perform specific purposes, they're not unique to only alias as they are used with almost every cli tool.
For instance,you can print out a list of file within a directory using the ls command, additionally you can use

ls -a
ls -la
to get similar but specific outputs.
Flags are usually accompanied with a - or -- symbol, to indicate they are optional.
There are two common flags when working with alias in linux, one is the:

  • -p flag: that prints out all user-defined alias in a reuseable format. A common use case is running alias -p which yields
alias pip='pip3'
alias pip2='pip'
alias push='git push -u origin master'
alias py='python3'
alias python2='python'
alias run='go run'
alias runapp='python manage.py runserver'
alias test='python -m unittest'
alias weather='curl wttr.in'
Enter fullscreen mode Exit fullscreen mode

on my pc obviously :)
Another common flag is the;

  • --help flag: that returns a help page, instructing how to navigate using alias. Running alias --help yields
$ alias --help
alias: alias [-p] [name[=value] ... ]
    Define or display aliases.

    Without arguments, `alias' prints the list of aliases in the reusable
    form `alias NAME=VALUE' on standard output.

    Otherwise, an alias is defined for each NAME whose VALUE is given.
    A trailing space in VALUE causes the next word to be checked for
    alias substitution when the alias is expanded.

    Options:
      -p    print all defined aliases in a reusable format

    Exit Status:
    alias returns true unless a NAME is supplied for which no alias has been defined.
Enter fullscreen mode Exit fullscreen mode

Unaliasing

Unaliasing is basically removing a defined alias, so that a command can't be reached via it's alias.
You can unalias a command by running:
unalias commandName
for instance i can unalias push in the above alias list by running:
unalias push
when i run push on the terminal again, i get:

$ push
bash: push: command not found
Enter fullscreen mode Exit fullscreen mode

obviously this isn't permanent as once the *.bashrc *_file is reloaded into the terminal memory again, all previously removed commands via **_unalias** are available again.
To permanently delete an alias, you would have to edit the .bashrc file and erase that specific command.

Adding Parameters to Alias with Shell Functions

I know! that dosen't sound right, alias do not accept parameters aside their regular pre-defined flags.
So how do we go about a case where we'd like to pass arguments to our alias?
For instance i usually prefer using an alias
build NameOfExedir/NameOfExe to build golang executables rather than typing go build -o NameOfExedir/NameOfExe -v .
so i'd rather do
build bin/myproject instead of go build -o bin/myproject -v .
In situations like this we apply the use of functions, although functions might be out of context for our current topic of discussion, i find them very useful when working with aliases.

Functions are commands in linux which are used to create functions or methods. Just like in many programming languages, functions are re-usable chunks of codes that carry out an instruction set.
There are two ways we can declare functions in .bashrc files,

  • Using a Function keyword: functions can be defined using the function keyword before specifying the function's name.
    Syntax
    function build { }

  • Using parenthesis: functions can also be defined using the function's name with parenthesis next to it.
    Syntax
    build() { }

Run help function for more information on working with shell functions.

Back to the above gobuild question;
we could create an alias that runs the go build functionality and then uses a function to parse the additional parameter, i.e the location the executable would reside. So:

alias gobuild="go build -o "
and then:

build(){
        gobuild "$1" -v .
        echo "Finished building project executable"
}
Enter fullscreen mode Exit fullscreen mode

so when i run, build bin/myproject
i get:

$ build bin/myproject
Finished building project executable

Enter fullscreen mode Exit fullscreen mode

and then when i run ls | grep 'bin'

$ ls | grep 'bin'
bin
Enter fullscreen mode Exit fullscreen mode

you can see there's a binary folder containing my project's executable, all done using a shell function that calls an alias to run a go build command within a directory of my choosing. This is resourceful as i can now use this build function in whichever project i'd like to workon.

Conclusion

In this short article we've seen how to optimize our development process by using aliases and functions to shorten long terminal commands.
I hope you enjoyed the article and in the meantime, let me know what you think about the article and possibly any amendments that could be made to improve it's user experience, thanks for reading and have a great time!

Top comments (0)