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.
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.
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.
source .bashrc
in .bash_profile
allowed the alias set in .bashrc to work.
– Treefish Zhang
Sep 03 '17 at 14:00
~/.bash_aliases
. That's the right way to do it. (read the .bashrc file for details).
– Emilio
Apr 14 '20 at 15:07
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.
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?
.bashrc
work while .profile
doesn't? Also, any reason why you chose to name the alias file .allias
rather than .alias
?
– Tom
Apr 04 '15 at 22:59
$ bash #start a new bash instance
This was problem with my server. Simple source .profile
didn't work for some reason
If you are using bash (echo $SHELL
) as a shell interpreter I think it is best (or maybe the only way) if you define your own CD function.
Edit your .bashrc
file and add the following at the end:
# my custom functions:
function gl () {
cd /home/jrenner/glances/glances/
}
You could also extend it like so:
# my custom functions:
function cdw () {
cd /home/jrenner/glances/glances/$1
ls
}
and change directly to a sub directory and list it's contents.
alias
directive placed in the .profile
file fails.
– Tom
Apr 04 '15 at 22:58
gl
? The terminal? The GUI? – Thomas Ward Mar 26 '13 at 02:58.profile
do not work in any context I tried: text console logins, graphical terminals, or anything else, even though enviromental variables from the same file do work. – Daniel Mahler Apr 06 '16 at 05:57