Skip to content

Instantly share code, notes, and snippets.

@MHMDhub
Last active April 13, 2021 13:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MHMDhub/d6778113ea6e2e3d8863e43ccc39bb68 to your computer and use it in GitHub Desktop.
Save MHMDhub/d6778113ea6e2e3d8863e43ccc39bb68 to your computer and use it in GitHub Desktop.
Reload .bashrc without logging out and back in:
==============================================
Two ways to do this:
1- source ~/.bashrc (or . ~/.bashrc)
2 - exec bash
With this, you won't even have to type "source ~/.bashrc":
Include your bashrc file:
alias rc="vim ~/.bashrc && source ~/.bashrc"
Every time you want to edit your bashrc, just run the alias "rc"
Details:
------
Both solutions effectively reload ~/.bashrc, but there are differences:
. ~/.bashrc or 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.
===================================================================
SETTING ENVIRONMENT VARIABLES:
=================================================================
#/etc/bashrc
This is where system wide functions and aliases good
Environment stuff goes in /etc/profile
It is NOT a good idea to change file /etch/bashrc. It's much better to create a custom.sh shell script in /etc/profile.d/ to make customer changes to your environment, as this will prevent the need for merging in future updates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment