Why is setting alias in .profile not working?
Solution 1
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 2
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 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.
Related videos on Youtube
talloaktrees
Updated on September 18, 2022Comments
-
talloaktrees over 1 year
I have this as the last line in my .profile:
alias gl="cd /home/jrenner/glances/glances"
yet even after reboot I get command not found when typing gl. What is happening? I am logged in as the correct user.
-
saji89 about 11 yearsAs for the formatting askubuntu uses markdown. Please check out askubuntu.com/editing-help for the details.
-
Tom about 9 yearsThat may be a fine suggestion, but it is not an answer, as it doesn't explain why the
alias
directive placed in the.profile
file fails. -
Tom about 9 yearsWhy does
.bashrc
work while.profile
doesn't? Also, any reason why you chose to name the alias file.allias
rather than.alias
? -
Treefish Zhang over 6 yearsVerified: adding
source .bashrc
in.bash_profile
allowed the alias set in .bashrc to work. -
Jason over 6 years#1 From what I understand .bashrc is specific to bash and .profile is specific to non-bash command line. #2 I don't know what you mean. I can spell after all. (edited)
-
Emilio about 4 yearsLooks like this should be the correct answer. Also, in Ubuntu just add your aliases in
~/.bash_aliases
. That's the right way to do it. (read the .bashrc file for details).