Bash: Saving aliases beyond one session

aliasbashsession

Recently, I've been fiddling around with Linux's terminal commands to try and get a better feel for the system.

I was happy to know that I could give commands a different name in order to call them, using the alias command. For example,

alias print="echo"

In this case, the echo would be replaced by print.

The only problem is that it only seems to stay for one terminal session. Without the use of third party software, is there a way that I can keep these aliases permanently? If there are software alternatives, I'll be glad to hear them.

I'm just looking for a way to do this without downloading anything.

Best Answer

You need to put your aliases in a file that will be read upon start of all sessions.

Your ~/.bashrc file should have the following:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

that means if you have a file ~/.bash_aliases file then it will be sourced and all the aliases defined in it will be applied in the session. It is the best practice to save your aliases in ~/.bash_aliases, if you don't have the file you can create it manually. As an alternative you can put your aliases in ~/.bashrc.

Also note that, if you want to permanently save the aliases those are defined only for current session of terminal, you can run:

alias >> ~/.bash_aliases