Aliases: difference between .bash_rc, .bash_aliases and /usr/local/bin

7,172

With the first method you are not creating an alias, you are creating a symlink. Symlinks are short for symbolic links:

Symbolic links are files that act as pointers to other files. [...] A symbolic link is a special type of file whose contents are a string that is the pathname another file, the file to which the link refers. In other words, a symbolic link is a pointer to another name, and not to an underlying object.

Read more about symlinks here and here.

Only with the second method you are, in fact, creating an alias.

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands (see SHELL BUILTIN COMMANDS below). The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the 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 using Terminal). 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.

Share:
7,172
Strae
Author by

Strae

Updated on September 17, 2022

Comments

  • Strae
    Strae over 1 year

    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:

    1. sudo ln -s /path/to/executable /usr/local/bin/desired_alias
    2. adding desired_alias = '/path/to/executable' to ~/.bashrc
    3. 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?

  • myusuf3
    myusuf3 almost 14 years
    @lilo you beat me to the tutorial! +1
  • Li Lo
    Li Lo almost 14 years
    @gc I'm hitting the sack so go wild :)
  • myusuf3
    myusuf3 almost 14 years
    @lilo hahahha :)
  • Strae
    Strae almost 14 years
    thanks, but why the symbolic link in /usr/local/bin is working as well?
  • Li Lo
    Li Lo almost 14 years
    You may think it's working the same, but it's not. Please read the materials on symlinks that I referred in my answer. Symlink and aliases serve different purposes and were created with different purposes in mind. To give you an example why symlinks are not working like aliases try this -- define an alias like: alias px="ps axu". Now try to accomplish the same with symlinks.
  • Strae
    Strae almost 14 years
    @Li Lo: i have understood the difference between synlink and /usr/local/bin, but my question (that's maybe a little bit off-topic with the general question) is: what is exaclty /usr/local/bin used for?
  • Li Lo
    Li Lo almost 14 years
    @daniel: /usr/local/bin: Binaries for programs local to the site (from "man hier"). For example if you would compile and install a program from sources it would go to /usr/local/bin.