27

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

0

4 Answers 4

50

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
2
  • 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. Oct 2, 2012 at 19:07
  • Not sure why...but /usr/share/doc/bash-doc/examples doesn't exist on my fresh installation (Digital Ocean, 20.04 LTS) - for anyone else that can't find it pull manually: sudo apt-get install bash-doc Sep 8, 2021 at 20:51
10

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.

5
  • 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. Sep 29, 2012 at 6:59
  • @Scott Severance. This may only be true on BSD then. I'll edit my answer.
    – Dean
    Sep 29, 2012 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. Sep 30, 2012 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, 2012 at 22:44
  • Great explanation of symlinks too Sep 8, 2021 at 20:55
4

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.

3

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 .

3
  • Great answer! Also, instead of a logout/login, you may run source ~/.bashrc to immediately apply the new .bashrc file.
    – SirCharlo
    Sep 28, 2012 at 19:27
  • @SirCharlo thank you , i have added from your comment :D
    – Raja G
    Sep 29, 2012 at 16:47
  • pretty simple and straigtforward
    – bhordupur
    Nov 9, 2016 at 11:39

Not the answer you're looking for? Browse other questions tagged .