Bash script to create an alias on Linux

Endrit Qerreti

Endrit Qerreti

In the previous tutorial, we covered how to create an alias on Linux, and I thought why not write a bash script that will do all the job for you, instead of writing commands and editing bashrc file, especially if you are not familiar with all this process.

So I wrote a little bash script, that will create an alias on your system and enable it on the same session.

How does this script work?

Once you run create_alias.sh, the script will prompt you with:

1) Alias word - This is the custom word that you can call to issue an x command

2) Alias command - Is the command that will be executed when you call the alias word

For example, Alias word - upgrade, Alias command 'sudo apt upgrade', so whenever you call upgrade the script will run sudo apt upgrade

Now that you know how the script work, let's create this script and make it executable so you can use it to generate aliases on your system

Step 1 - Create create_alias.sh

create_alias.sh is the name of the script. I called it create_alias because that's what it does, it creates an alias on linux.

You can create this file with any text editor, using GUI or terminal, that's you choice. However, we went with terminal and touch command

sudo touch create_alias.sh

Once you run the command above, it will create the file on the same path from where you ran this command.

So if you created this file on root then you will need to go on root and copy the bash script into it.

Step 2 - Write bash script

Before adding the whole code here, let's go through the script and explain what each line does.

First the script asks you to enter the alias word

read -p "Alias word: " alias_word

Next, it asks you to enter the alias command

read -p "Alias command: " alias_command

Now, once script has both inputs, alias_word and alias_command, then the script moves to the next step and uses this information to create the alias.

 sudo tee -a ~/.bash_aliases 

We are using tee command to append this input to the ~/.bash_aliases

Once script appends this line into the bash_aliases file, to be able to use this alias, we need to source it in the current shell. If we don't do this, then you won't be able to use your alias on the same terminal and session, but you can use it next time you login on your system.

. ~/.bash_aliases
or
source ~/.bash_aliases

Step 3 - Make create_alias.sh executable

Making bash script executable is necessary in order to run the script, to make create_alias.sh executable run

chmod +x create_alias.sh

Step 4 - Run create_alias.sh

Now you can run the script

. ./create_alias.sh

Notice that we are using . instead of source but you can use source as well, for example another way of running this is script is

source ./create_alias.sh

source is the synonym for the . in linux, so both ways work fine.

Once you run the script you should see this window

Now you need to enter the alias word, and then the alias command

Press enter for each input and the script will show on the log when alias has been added as shown in the screenshot below

Next all you have to do is run the alias you created, for example the alias that I created above is called update, so when you run update, the command sudo apt update will get executed

How to delete aliases temporarily

To delete aliases is very easy, you can use the unalias command

unalias update

Note: update is the name of the alias that we created, the command above will remove this alias from your shell configuration file

Verify that alias is removed

To see if the alias is removed successfully, you can try to run the alias command you created and see if it works, or simply run alias and it will list all the alias available

alias

unalias command will not remove aliases permanently, to remove aliases permanently you need to delete them from the ~/.bash_aliases file

Delete Aliases Permanently

1) Open ~/.bash_aliases with a text editor such as vim,nano

sudo vim ~/.bash_aliases

2) Delete or comment the alias line that you added, for example

#alias update='sudo apt update'

3) Save changes by pressing CTRL +X and exit

4) Once you have deleted or commented the added line, run the command below to save changes

source ~/.bash_aliases

5) Verify that the aliases is deleted successfully using methods we mentioned above

Download the full bash script here

./ create_alias_.sh

Buy for $1

Conclusion

We wrote a  bash script that allows you to create an alias on Linux without having to edit the ~/.bashrc or ~/.bash_aliases file manually to append a new alias.

We  explained step by step how the script works, and how to use it. We also covered how to remove aliases temporarily using the unalias command, and how to delete an alias permanently.