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.
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
.bash_aliases file before getting there.
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.
.bashrc file does a fine job of loading the
.bash_aliases file like so:
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.
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:
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:
- $1 is a positional parameter that returns the first argument passed. Since “palias” is an alias to a function call, it returns the first word after the word “palias”, without the inverted commas.
palias foo='bar'will return foo=bar as $1.
Interestingly, $0 returns the address of the script or shell running. In this case /bin/bash
- $KEY and $VALUE variables are defined by splitting the string in $1 at the first “=”. The string $1 is passed to cut and with delimiter (-d) as “=”, fields (-f) 1 and 2- are selected
echo "foo='bar'" | cut -d"=" -f 1 has the same effect as
echo "foo='bar'" | cut --delimiter="=" --fields 1
echo "foo='bar==car'" | cut -d"=" -f 2- fetches ‘bar==car’ while
echo "foo='bar==car'" | cut -d"=" -f 2 fetches ‘bar
- Once KEY and VALUE are abstracted, they are appended to the
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.
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fiallows you to use your newly declared permanent alias in the running terminal too, by loading the bash_aliases file again. Not doing this, would result in restarting of terminal for changes to take effect. (Thanks Vinayak for pointing out)
- For single word aliases, scrapping the inverted commas would do.
- For special characters like “ ( , ) , @” etc, scrapping the inverted commas would not work
- Garbage after the first space won’t matter.
- Spaces around the “=” in
palias foo = 'bar'wouldn’t work. They wouldn’t work with regular alias either, for that matter.
If you’re still reading, I hope you found it useful. Till next time! :)