Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Eliah Kagan, Eric Carvalho, David Foerster, bodhi.zazen, mikewhatever Apr 11 '15 at 9:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

As for this particular example, ^L (Control-l) clears the screen as well. – loevborg Aug 6 '10 at 16:10
up vote 104 down vote accepted

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

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
share|improve this answer
+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 6 '10 at 21:50
example: echo "cls='clear'" >> ~/.bash_aliases && source ~/.bash_aliases – hobs Sep 10 '12 at 15:56
@æ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 7: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

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.

share|improve this answer
Thanks, it worked when I wrote in ~/.bachrc P.S. There is no ~/.profiles in my home directory. – Zango Aug 6 '10 at 15:31
.profile might be .bash_profile now – txwikinger Aug 6 '10 at 15:32
If the file in question does not exist, you can simply create it. – Ryan Thompson Aug 6 '10 at 18:03
Thanks, I was wondering what's the difference between those two. (bashrc and bash_profile) – emf Oct 6 '10 at 20:28 for the difference between ~/.bash_profile and ~/.bashrc – Avenger Jun 7 at 8:12

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.

share|improve this answer
Perfect! exactly what I was looking for. I renamed it "mkalias". – TecBrat Jun 29 '13 at 14:57
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

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

share|improve this answer
Why not ~/.bashrc? – mac9416 Aug 6 '10 at 15:23
bashrc is preferred, I understand, though not clear on why – emf Oct 1 '10 at 18:46

See 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
share|improve this answer
echo "Enter alias like this:"
echo "alias dir='ls'"
read var
echo "$var" >> /home/$user/.bashrc && . ~/.bashrc
cat .bashrc
share|improve this answer
Please explain, what your answer does. Just showing a bunch of code is almost never good. IF you do so, ping me with a comment to @DavidFoerster and I'll vote for your answer. – David Foerster Apr 16 '15 at 9:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.