When I define a new alias in .bash_aliases file or a new function in .bashrc file, is there some refresh command to be able immediately use the new aliases or functions without closing the terminal (in my case xfce4-terminal with a few tabs open, many files open and in the middle of the work)?

  • 6
    Note that there is no command that will made that new command known in all your open terminals/tabs. You'll have to do the . .bashrc or source .bashrc in every shell you have open. – Paul Tomblin Oct 19 '11 at 12:05
up vote 52 down vote accepted

Sourcing the changed file will provide access to the newly written alias or function in the current terminal, for example:

source ~/.bashrc

An alternative syntax:

. ~/.bashrc

Note that if you have many instances of bash running in your terminal (you mentionned multiple tabs), you will have to run this in every instance.

  • 3
    source is csh-derived. The bourne shell way is . .bashrc. – Paul Tomblin Oct 19 '11 at 12:04
  • That's interesting that this doesn't work when I define alias prg='prg.py' . I have to close terminal. – xralf Oct 20 '11 at 11:49
  • "you will have to run this in every instance." - Note that zsh users can set TMOUT and TRAPALRM appropriately to stat and (if necessary) re-source ~/.zshrc once per second, or at any other reasonable interval. I don't believe bash can do this, though. – Kevin Nov 11 '16 at 0:18

Typing . ~/.bashrc at the command line will run .bashrc and so any functions defined in that file will be created.

.bashrc itself will then also call and run .bash_aliases (if it exists) if .bashrc has this code in it:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

whereas using . ~/.bash_aliases alone (at the command line for example) will just try and run .bash_aliases without involving .bashrc and will give an error if the file doesn't exist (hence the file check test when in .bashrc).

Sometimes you will want to turn an alias into a function, but when you source the bashrc file, a weird error may occur, something like this:

. ~/.bashrc
bash: /home/username/.bashrc: line 38: syntax error near unexpected token `('
bash: /home/username/.bashrc: line 38: `hello_world() {'

This happens because the alias name is clashing with the name of the newly defined function. As far as I know, to avoid this one has two options:

The first method is to use exec bash, the drawback here is that you loose your environment:

bash-4.3 $
[1]: env_var="a value"
-----------------------------------------------------------------------
bash-4.3 $
[2]: echo $env_var
a value
-----------------------------------------------------------------------
bash-4.3 $
[3]: exec bash
-----------------------------------------------------------------------
bash-4.3 $
[1]: echo $env_var

-----------------------------------------------------------------------

So this leaves us with the second method, which is to unalias everything, then source the bashrc file. This preserves our environment (with the notable exception of aliases):

bash-4.3 $
[1]: unalias -a && . $HOME/.bashrc

Normally I have this as an alias in my bashrc by the name of reload.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.