I have the following line in my .profile
file in my home directory:
alias flux='redshift -O 2500'
But whenever I login and type flux
I get a flux is not a recognized command
.
I then do a source .profile
and type flux
again and it works perfectly.
I am on Ubuntu 20.04
Why is the alias
in my .profile
not working unless I do a source .profile
?
edit: I understand now that .profile
is not the correct place to put this.
I ended up loading aliases from the .bashrc
file using the source
command:
for file in ~/.dotfiles/.{exports,aliases}; do
[ -r "$file" ] && source "$file"
done
unset file
Best Answer
TL;DR: As Gunnar Hjalmarsson says, put aliases in
~/.bash_aliases
.The commands in
~/.profile
are run by login shells. That file is a good place to do things that should be done once at the beginning of each session but not repeated every time a new shell is started.~/.profile
is good for setting environment variables, which will be inherited by all descendant processes.Aliases are not inherited, and the shell you get in a terminal window in Ubuntu is not (by default) a login shell. To define aliases for all your interactive Bash shells, the definitions need to be somewhere where they'll be run each time you start such a shell.
The usual places are either in:
~/.bashrc
~/.bash_aliases
New interactive non-login shells source
~/.bashrc
(as do some noninteractive shells). Your~/.profile
, by default, sources~/.bashrc
.The default
~/.bashrc
in Ubuntu checks if~/.bash_aliases
exists and sources it.I recommend putting new aliases in
~/.bash_aliases
.Sourcing
~/.profile
works to define the aliases, but it also re-runs everything else in that file, most of which you don't need to run again, and some of which might do things you don't want. For example,~/.profile
checks if~/bin
exists and, if so, prepends it to your$PATH
. If you source~/.profile
multiple times, you can get the same directory appearing multiple times in$PATH
. This would have to happen quite a lot to cause a significant performance problem. But even one extra occurrence can cause confusion when you're inspecting the output of a command likeprintenv PATH
.If your aliases aren't defined even in initial login shells, such as when you log in from a virtual console or via SSH, then as Kulfy alludes to the issue may be that you have a
~/.bash_profile
or~/.bash_login
file. If such a file exists, Bash login shells source it instead of~/.profile
. Most often you would not want to have either such file on Ubuntu, and if you did, you would most likely want it to source~/.profile
so that the commands in~/.profile
still run.But even if that is the case, you shouldn't define aliases in
~/.profile
(nor in~/.bash_profile
or~/.bash_login
), as that doesn't define them in non-login shells.