DEV Community

Cover image for Linux aliases set up simple!
Ivaylo Ivanov
Ivaylo Ivanov

Posted on

Linux aliases set up simple!

Hi guys, today I will show how easy it is to set up aliases in almost any Linux distro. Before we begin let's clarify what are aliases in the bash.

Alias is a command that enables replacing a word or set of letters with a bash command. Often it is used for simplifying the workflow in the command line, for example, we can come up with an abbreviation that is going to equal multiply commands. Without further due let's hop into the setup process.

To create an alias there are 3 easy steps:

1. Navigate to your home directory.

To navigate to your home directory using the cd command.

cd /home/your_username
Enter fullscreen mode Exit fullscreen mode

2. Add your alias to the .bashrc file.

After you have navigated to your home directory, open the .bashrc file with nano.

sudo nano .bashrc
Enter fullscreen mode Exit fullscreen mode

Now go to the bottom of the file and type alias, then the name of the custom command and the Linux commmand/s it represents. Take in mind that there are no spaces between the equal sign, otherwise it is not going to work.

.bashrc file

You can add as many custom commands as you want. Every command should be on a new line. After you are done, save the file and exit.

3. Activate the custom commands you've added.

Probably you have noticed after saving the file when you try to run a custom command it doesn't work that is because we have not activated them yet. To activate just type in the command line source and .bashrc.

source .bashrc
Enter fullscreen mode Exit fullscreen mode

And you are ready! Now you can run any of the custom commands you have created
If you have any questions, comment them down I will answer them as soon as possible.

Discussion (4)

Collapse
anonyco profile image
Jack Giffin • Edited on

Two important edits:

  1. Combine steps one and two. There's no reason to separate them:

    nano ~/.bashrc
    

    For step #3:

    . ~/.bashrc
    

    . should be introduced to readers, not source. The source command is a bashism and should be avoided in favour of . so that readers gain a more generalized and useful knowledge.

  2. Clarify that alias expansion is not standard POSIX and only works for bash in conjunction with the shopt -s expand_aliases bash option (which happens to be enabled by default). It's safe if you follow your steps because only bash will read ~/.bashrc, but I and some other people take examples from posts we read and apply them elsewhere, so this is important notice.

Collapse
ivayloiv profile image
Ivaylo Ivanov Author

Thank you very much for your feedback. I will take it into account.

Collapse
didiiv profile image
DidiIv • Edited on

💥You are super guy!💥 Go ahead! 🌟

Collapse
ivayloiv profile image
Ivaylo Ivanov Author

Thank you very much! Glad you liked my content. ❤️