Why is setting alias in .profile not working?
Solution 1:
There are two related reasons why aliases don't always work when put in the .profile
file. The first is that the .profile
(or .bash_profile
) file is only run for a login shell. If you are starting bash in a terminal window under X, your terminal emulator (e.g. gnome-termanl) probably isn't running bash as a login shell. [Most have an option to change this if you want but the default (for gnome-termal anyway) is not to run it as a login shell.]
The shell will be an interactive shell and so .bashrc
will be run.
However, normally bash has been run as a login shell back when the X session was being started. So if there are alias commands in .profile
they will have been executed along with setting environment variables like the PATH etc. When a terminal window is opened a new instance of bash is run to prompt for, and execute commands in that terminal window. Unlike environment variables, aliases can not be exported from one instance of bash to a new one started by it. So the aliases are not passed on to the new shell.
To see this, try this experiment:
export ROBERT=bob
alias james=jimmy
echo $ROBERT
alias james
bash #start a new bash instance
echo $ROBERT
alias james
exit #end the new bash instance and revert to the original one
echo $ROBERT
alias james
Note that .bashrc
is not run by bash when it is started as a login shell. So putting your aliases there won't always work unless your .bashrc
is sourced from your .profile
, which is a very common practice.
Solution 2:
I'm pretty sure that lpanebr's idea will work, but here's a more elegant solution. Do that alias command in .bashrc
That's how I do it, or some people prefer to add a file dedicated to alias. Call it .alias
or whatever and add .alias
to your .bashrc
Wish I could do formatting like @lpanelbr. I wonder if there is a wiki?
Solution 3:
The right way to do this in Ubuntu is to add your alias to ~/.bash_aliases
. Create the file if it doesn't exist.
This file (if present) is called from the default ~/.bashrc
, and the alias will be available in your terminal emulators too.