Playing around with Terminal, I noticed that there are many ways to create permanent aliases.
I'm a Linux newbie, and from what I know, doing:
sudo ln -s /path/to/executable /usr/local/bin/desired_alias
- adding
desired_alias = '/path/to/executable'
to~/.bashrc
-
uncommenting those lines in
~/.bashrc
:if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
and putting
desired_alias = '/path/to/executable'
into the~/.bash_aliases
all have the same effect.
What is the difference between the first and second methods?
Best Answer
With the first method you are not creating an alias, you are creating a symlink. Symlinks are short for symbolic links:
Read more about symlinks here and here.
Only with the second method you are, in fact, creating an alias.
You can define an alias anywhere where you can type a command and have the shell (bash in this case) interpret it, however in order for the alias to be available in other shells it needs to be defined in a file that's interpreted by the shell on startup (shell startup, not computer startup).
For bash these are
/etc/bash.bashrc
(system wide) and~/.bashrc
. These files are interpreted when the shell starts in interactive mode (like when usingTerminal
). I'm not going to mention the profile files because they serve a different purpose.So, you want to add your aliases to
~/.bashrc
to have them available in every interactive shell.The
.bash_aliases
method accomplishes exactly the same thing as putting the aliases in~/.bashrc
but has the added benefit of being easier to be parsed and manipulated by programs.The
. ~/.bash_aliases
means source (load)_~/.bash_aliases_
in the context of the currently running shell.