Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

I want to save my alias commands forever like the default ones when I type "alias" so I don't retype them every time I leave my bash

share|improve this question

marked as duplicate by Radu Rădeanu, user68186, Kevin Bowen, Thomas W., psusi Jun 19 '13 at 18:18

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.

4 Answers 4

up vote 31 down vote accepted

In Ubuntu, the default .bashrc skeleton file looks for a .bash_aliases file in your home directory when you log in and sources it. So if you just create a .bash_aliases file and put any aliases you want in it, it should be sourced automatically when you open a new bash shell (no need to log out of desktop and back in, just open a new terminal). Here's the relevant part in the default .bashrc file:

# 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
share|improve this answer
1  
Although the convention has been to put them in .bashrc directly, this is obviously a better way to do it with Ubuntu, as it is specifically stated as such at the top of .bashrc. And if you change .bash_aliases while running a terminal, you can enter source ~/.bash_aliases to reload the aliases without needing to close the terminal. –  Marty Fried Oct 2 '12 at 19:07

You have to save your alias's at .bashrc file in your home directory.

Open your terminal and type this

gedit .bashrc

it will open a text file and in that text file place your alias command at the last line and save it and close .

Logout and login to apply changes and check .

edit :Also, instead of a logout/login, you may run source ~/.bashrc to immediately apply the new .bashrc file

Hope that helps .

share|improve this answer
    
Great answer! Also, instead of a logout/login, you may run source ~/.bashrc to immediately apply the new .bashrc file. –  SirCharlo Sep 28 '12 at 19:27
    
@SirCharlo thank you , i have added from your comment :D –  AgentCool Sep 29 '12 at 16:47

First copy all your existing aliases to a new file, e.g. ~/.bash_aliases. The simplest way to do this is to execute alias > ~/.bash_aliases. You then remove them from ~/.bashrc and replace by a call to your new file (. ~/.bash_aliases). If fact, you may find that there's a commented section to do this already; just uncomment it.

You then need to create (or edit) the file bash executes when it logs out, i.e. ~/.bash_logout and simply put the command alias > ~/.bash_aliases in it. This will overwrite your old set of aliases with the currently defined set ready for your next login each time you logout.

Of course, if you open multiple sessions and define different aliases in each, then you will need something more complicated to handle this.

share|improve this answer

Create a file called something like runthisstuff

In this file, stick a bunch of commands:

alias doc='cd ~/Documents/'
alias ps='ps aux'
date

Now run the file like so:

$ source runthisstuff

It will print the date, and you can now use the aliases. The date command is just to show you that you can pretty much stick anything you want in there, and it will run.

Now you still have to manually source this file, which isn't so convenient. Luckily, when you start up a new shell, there are a few files like this that are automatically called. Since you're using a bash shell, ~/.bashrc is automatically sourced. As Ian B. pointed out, in the default .bashrc there's already a part that checks to see if a file called ~/.bash_aliases exists, and sources it if it does.

So, you might as well stick your aliases in ~/.bash_aliases

If you want your aliases to be extra permanent, you can always create a .bash_aliases file in a Dropbox folder or the like, and create a symlink to that file:

ln -s ~/Path-to-dropbox-aliases/.bash_aliases ~/.bash_aliases

This will let you easily share them across machines.

share|improve this answer
1  
It isn't true that creating files with names beginning with a dot allows you to source those files. You can source files without regard to name. The only thing is that it's conventional for settings files such as this to be dot files. –  Scott Severance Sep 29 '12 at 6:59
    
@Scott Severance. This may only be true on BSD then. I'll edit my answer. –  Dean Sep 29 '12 at 15:37
1  
I just tested it in FreeBSD and found that it likewise is untrue there. During my tests, though, I discovered that the source command searches the PATH for its command, which caused errors if the filename I tried to source was the same as that or a program on my PATH (such as test). This is certainly strange behavior for which I can't imagine a useful purpose, but it remains that it is possible nonetheless to source non-dotfiles. –  Scott Severance Sep 30 '12 at 1:12
    
@ScottSeverance Right you are. I had used the same filename as a script I had in my PATH. That script didn't exist on the Linux box I tried on. I've removed the incorrect info. –  Dean Sep 30 '12 at 22:44

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