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
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
Create a file called something like
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
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.
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.
You have to save your alias's at
.bashrc file in your home directory.
Open your terminal and type this
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 .