loading...

Linux Mint – Making aliases persistent

Installing Apache On CentOS 8

There is one considerable downside to aliases. If you were to log out of your machine, all the custom aliases you’ve created would be wiped out. Aliases that you create do not survive once your session has ended. Therefore, if you would like to make an alias or two (or many) permanent, there’s another step in the process.

There are many ways to make aliases permanent, but the generally accepted method (which is possibly the easiest) is to edit your .bashrc file and add the aliases to it.

Before we continue with the concept of making aliases permanent, the concept of Bash configuration files should be explained. The .bashrc file is one of several files used by Bash (the Linux shell) for you to store various customizations. There are several of these configuration files, each with a specific purpose, and each is read by the system at a specific time.

In your home directory, one or several of these configuration files may be present. To see which ones you have in your home directory, consider the following variation of the ls command:


ls -la ~ | grep bash

With the previous command, we’re executing the ls command with the -la flag against the home directory as designated with the ~ character. The -l flag means we want a long list (this is not required; it is just easier to read), and the -a flag means that we want to see all the files, even those that are hidden (as these configuration files typically are). Next, we pipe the output of the ls command (using the | character) to the grep command, clarifying that we want to see only the output that includes the word bash. Put it all together, and you will see all the files containing bash in their names stored in the root of your home directory.

At first you’ll probably only see the .bash_logout and bash_history files. By default, Linux Mint does not create the .bashrc file in your home directory. However, if you create it yourself, it will be recognized and used by your system.

Note

The .bash_history file shows a history of all the commands you’ve typed, though it is not relevant to this section. Still, it may be worth checking out, so feel free to take a look at its contents.

The .bashrc file is known as a non-login configuration file. This means that it is not read when you log in to your desktop environment. Instead, it’s read each time you open a terminal shell. This is perfect for our needs to make aliases permanent, because this means that each time we open a shell, any alias-creation commands we place in the .bashrc file will essentially be run for each shell window.

To get started, first let’s edit the .bashrc file. You can do so with a graphical text editor or even in the shell itself. If you don’t have a .bashrc file in your home directory (there’s a good chance you won’t), go ahead and create it. You can use the following command for creating and editing it at the same time:


nano ~/.bashrc

Depending on whether the .bashrc file already existed or not, you may either end up with an empty window, or it may contain text. Either way, add your alias commands to the end of the file, one per line. When you have done this, save the file. In the case of nano, you can save the file by pressing Ctrl + O and then Ctrl + X. If the .bashrc file didn’t exist when you went to edit it, you should make the file executable as well using the following command:


chmod +x ~/.bashrc

Assuming you didn’t make any typos, all of the aliases you created will survive between sessions from this point forward. However, as we created the .bashrc in our local home directory, these changes apply only to our user account. If someone else were to log in to the system, they wouldn’t benefit from these aliases. This may be exactly what you want, but there is a way to activate aliases for all users. If you are configuring Linux workstations or servers in a corporate environment, this may be ideal. To create aliases for all the users instead of only for yourself, edit the /etc/bashrc file instead of ~/.bashrc, using the following command:


sudo nano /etc/bashrc

As with the .bashrc file we edited earlier, the /etc/bashrc file is unlikely to exist on your system. However, the same direction still applies. Simply add your alias-creation command lines to the end of the file, save it, and make it executable if it didn’t already exist. The main difference here is that we need to use sudo, since this is a system-wide file. Once you’ve added your aliases to the file, all the users on the system will be able to benefit from them.

Comments are closed.

loading...