Nihal Singh

Coding enthusiast

Playing with aliases



Since I can’t do without my games, I can’t do without Windows, and I have to dualboot. This means that most of my data is stored on a disk shared between both the operating systems. The path to the directory where I keep my projects thus becomes rather long. And I like to keep an alias for it.

alias code='cd /media/Nihal/Code'

What this would do is create an alias by the name of ‘code’ which would exist till the time you killed the terminal. Rather I have created a permanent alias for it, which is stored by copying the above line into the .bash_aliases file in the home directory (if it doesn’t exist, you should really create one). I wanted to create a few more permanent aliases as and when needed and the process to do so felt tedious. Alas! I wrote another alias for it. Let’s talk about the .bashrc and .bash_aliases file before getting there.

The .bashrc file

In the home directory there also exists a .bashrc file which shall contain a lot of oher things. The .bashrc file is a script which executes evereytime a terminal is started in the interactive mode. It contains a set of configurations for the terminal. The .bashrc file provides a space to set up variables, functions, aliases etc that you may want to use. The .bashrc is then run every time you open up a new terminal. Be careful, that any error or change you make in the .bashrc file will be reflected in all subsequent terminal windows launced. Also if you have a terminal window running, and you make changes to the .bashrc file thereafter, you will obviously have no effect on the running terminal and you may want to restart it.

Loading the .bash_aliases file

The .bashrc file does a fine job of loading the .bash_aliases file like so:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

This essentially allows .bashrc to load the ‘.bash_aliases’ file within itself, preventing you from meddling with it and managing the list of aliases neatly.

Creating a Permanent Alias

Creating a permanent alias is easy. Just open up ~/.bash_aliases and append the following line to it.

alias foo='bar'

Whenever you type in “foo”, “bar” will be passed as an input to the terminal.
Note: You can do this with ~/.bashrc as well but it is neater not to.

A perhaps better way to do this would be to automate the task and save yourself the hassle of opening a the fine and appending to it manually. And what better way to create aliases by using another alias? I wanted to have a way to create a permanent alias by just typing in a ‘p’ before alias like so:

palias foo='bar'

To do this, I opened up ~/.bash_aliases and created a function called permanent_alias and called it every time I entered “palias” on the terminal:

permanent_alias(){
    KEY=$(echo $1 | cut -d"=" -f 1)
    VALUE=$(echo $1 | cut -d"=" -f 2-)
    echo -e "\nalias $KEY='$VALUE'" >> /home/nihal/.bash_aliases
    echo "New permanent bash alias set: alias $KEY='$VALUE'"
    if [ -f ~/.bash_aliases ]; then
	    . ~/.bash_aliases
	fi
}

#Create alias for function call
alias palias='permanent_alias'

Explanation

echo "foo='bar'" | cut -d"=" -f 1
#returns "foo"
echo "foo='bar'" | cut -d"=" -f 2
#returns "bar"

Note: echo "foo='bar'" | cut -d"=" -f 1 has the same effect as echo "foo='bar'" | cut --delimiter="=" --fields 1
Note: echo "foo='bar==car'" | cut -d"=" -f 2- fetches ‘bar==car’ while echo "foo='bar==car'" | cut -d"=" -f 2 fetches ‘bar

echo -e "\nalias $KEY='$VALUE'" >> /home/user/.bash_aliases

Note: The -e flag is for enabling interpretation of backslash escapes like “\n” (newline). echo "string" >> file is used to append “string” at the end of file.

Usage

palias foo='bar'
and
palias foo=bar
would have same effect as we manually add inverted commas later.
	However,
palias foo='cd /home'
#$1 = "foo=cd /home"
and
palias foo=cd /home	
#$1 = "foo=cd"
would not have the same effect.
palias foo='permanent_alias()'
and
palias foo=permanent_alias()
would not have same effect as "(" would be an unexpected token.
palias foo='bar'
and
palias foo='bar' this is an amazing blog
would have same effect.

If you’re still reading, I hope you found it useful. Till next time! :)