How do I reload .bashrc without logging out and back in?

如果我对.bashrc进行更改,如何重新加载而不注销并返回?


You just have to enter the command:

source ~/.bashrc

or you can use the shorter version of the command:

. ~/.bashrc

or you could use;

exec bash

does the same thing. (and easier to remember, at least for me)

exec command replaces the shell with given program, in our example, it replaces our shell with bash (with the updated configuration files)


To complement and contrast the two most popular answers, . ~/.bashrc . ~/.bashrc and exec bash :

Both solutions effectively reload ~/.bashrc , but there are differences:

  • source ~/.bashrc will preserve your current shell :

  • Except for the modifications that reloading ~/.bashrc into the current shell (sourcing) makes, the current shell and its state are preserved , which includes environment variables, shell variables, shell options, shell functions, and command history.
  • exec bash , or, more robustly, exec "$BASH" [1], will replace your current shell with a new instance, and therefore only preserve your current shell's environment variables (including ones you've defined ad-hoc).

  • In other words: Any ad-hoc changes to the current shell in terms of shell variables, shell functions, shell options, command history are lost.
  • Depending on your needs, one or the other approach may be preferred.


    [1] exec bash could in theory execute a different bash executable than the one that started the current shell, if it happens to exist in a directory listed earlier in the $PATH . Since special variable $BASH always contains the full path of the executable that started the current shell, exec "$BASH" is guaranteed to use the same executable.
    A note re "..." around $BASH : double-quoting ensures that the variable value is used as-is, without interpretation by Bash; if the value has no embedded spaces or other shell metacharacters (which is not likely in this case), you don't strictly need double quotes, but using them is a good habit to form.

    链接地址: http://www.djcxy.com/p/25324.html

    上一篇: 什么是与Python的subprocess.check等效

    下一篇: 如何重新加载.bashrc而不注销并返回?