Create PATH shortcuts in BASH - Ubuntu

In this article, we would discuss how we can easily create PATH shortcuts in BASH. BASH – Bourne Again Shell, is a command line interpreter. Furthermore, if we talk of shell – in simple language, it allows us to execute commands.

Few articles back we discussed creating command shortcuts using alias – a built-in shell command – Create and use alias in Ubuntu.

This one is different from command shortcuts. Herein, we intend to have PATH with filename assigned to the variable.

Consider a scenario, wherein we have to enter a particular PATH/filename repeatedly in the shell. Although, nothing wrong with that. But, it hampers our productivity. We could have devoted time lost to other things and would have performed better.

In those cases, PATH shortcuts can really help. We understand with couple of examples.

Create PATH shortcuts in BASH

Let’s say we want to create a shortcut for /etc/hosts file. We don’t want to type the entire filename every now and then. Therefore, we store the entire PATH and filename in a variable. Just make sure that, the variable name shouldn’t correspond to any other variable used by the system.

hs=/etc/hosts

Now, we need to the add the variable to the environment. This can be done using export command.

export hs

To verify if everything has gone the way we want them to be –

echo $hs

it should return with the output –

/etc/hosts

Use cat command-line utility to display the contents of the file –

cat $hs

Now, see the difference – we haven’t provided the complete file name. Just the variable name. But, the variable will be lost if we log out from the shell. To make the changes permanent, we need to append the ~/.bashrc configuration file.

To append, use a text editor like nano

nano ~/.bashrc

Thereafter, append the file with the following code containing the variable –

hs=/etc/hosts

save and exit. To make changes in the current shell.

source ~/.bashrc

To verify if everything went as intended –

echo $hs

In conclusion, if the variable is already defined somewhere then issues may crop up. This will not be fruitful in those cases. Apart from that, it is great to have PATH – filename shortcuts to make things easier for us.

Similar Posts