252

If you create an alias for example:

alias cls="clear"

It exists untill you kill terminall session. When you start a new terminal window the alias doesn't exist any more. How to create "permanent" alias, one that exists in every terminal session?

Zango
  • 5,011

5 Answers5

283

You can put such aliases in the ~/.bash_aliases file.

That file is loaded by ~/.bashrc. On Ubuntu 10.04, the following lines need to be uncommented to enable the use of ~/.bash_aliases. On Ubuntu 11.04 and later, it's already enabled:

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

The aliased command will be available on any new terminal. To have the aliased command on any existing terminal one need to source ~/.bashrc from that terminal as,

source ~/.bashrc
sourav c.
  • 44,715
PHP Guru
  • 2,846
  • 13
    +1 I recommend this over editing ~/.bashrc. While indeed useful for a variety of other purposes, ~/.bashrc just has too many elements that could throw off a user who is unfamiliar with the peculiarities of Linux shells. – ændrük Oct 06 '10 at 21:50
  • 6
    example: echo "cls='clear'" >> ~/.bash_aliases && source ~/.bash_aliases – hobs Sep 10 '12 at 15:56
  • 4
    @ændrük I actually find the profusion of shell config files confusing. In my mind it is easier if there is one fairly long config file with all the settings. – haziz Dec 13 '12 at 07:14
  • 14
    @hobs it must be: echo "alias cls='clear'" >> ~/.bash_aliases && source ~/.bash_aliases – Amir Ali Akbari Aug 15 '13 at 10:30
  • gracias for the correction – hobs Aug 16 '13 at 17:48
  • 1
    In Ubuntu 14.04, putting the aliases in a ~/.bash_aliases doesn't seem to work, but there is this line instead: test -s ~/.alias && . ~/.alias || true. So it works if I put them in the ~/.alias file! – MakisH Nov 12 '15 at 17:18
  • 3
    ok so this topic is almost 7 years old now and I can't seem to find any recent answer like this. however it doesnt work for me anymore and asking now if something has changed – TheDefinitionist Oct 05 '16 at 13:00
  • A correction in above comment that I can't edit echo "alias cls='clear'" >> ~/.bash_aliases && source ~/.bash_aliases – 1UC1F3R616 Jan 17 '21 at 21:23
48

Add your line into ~/.bashrc or into ~/.profile / ~/.bash_profile for remote logins.

If you want the command being executed for all users, put it into /etc/bash.bashrc.

Edit: In the latest versions of Ubuntu, ~/.bashrc automatically sources ~/.bash_aliases, so permanent aliases are best put into this file instead.

txwikinger
  • 28,462
20

You can add the function below to your .bashrc file.

function permalias () 
{ 
  alias "$*";
  echo alias "$*" >> ~/.bash_aliases
}

Then open a new terminal or run source ~/.bashrc in your current terminal. You can now create permanent aliases by using the permalias command, for example permalias cls=clear.

Tolli
  • 482
  • 4
    Usage Note: when I typed mkalias smount='sudo mount' the quotes were not litterally echoed, so my solution was mkalias "smount='sudo mount'" If you are aliasing a 2+ word command you'll need this too. – TecBrat Jun 29 '13 at 22:04
  • I created a gist for this: https://gist.github.com/Masterxilo/f1967743fda3a1aded56ebaff4dd097b . Install permalias for the current user using { curl -s https://gist.githubusercontent.com/Masterxilo/f1967743fda3a1aded56ebaff4dd097b/raw/permalias | source /dev/stdin ; source ~/.bashrc ; } – masterxilo Nov 20 '18 at 20:34
  • 1
    However, I created permfunction, a more powerful alternative https://gist.github.com/Masterxilo/29ac0df083827bbd45a7c8ddcf3936d7 which creates globally installed scripts on the PATH instead. These will be available to all open sessions immediately and are much more flexible. Install using curl -s https://gist.githubusercontent.com/Masterxilo/29ac0df083827bbd45a7c8ddcf3936d7/raw/permfunction | sudo -E bash - ; hash -d permfunction &> /dev/null || true – masterxilo Nov 20 '18 at 20:36
6

See http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html for the difference between ~/.bash_profile and ~/.bashrc

~/.bashrc is run every time you open a new terminal, whereas ~/.bash_profile isn't. ~/.bashrc contains the following, which includes the ~/.bash_aliases file. This would be the most appropriate place to add your alias.

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
Mat
  • 73
5

Stick that command in the last line of your ~/.bash_profile

popey
  • 23,667