3

While I understand that source command executes a program in the current shell, I don't quite understand why we need to run source .bashrc to "reload" the .bashrc file.

From my understanding, we do not run any script by adding an alias to .bashrc - with source .bashrc we just "reload" the file.

Why does it not reload automatically?

Probably, I'm missing something.

How does it work under the hood? Why do we need to source this file to get new aliases working without login out?

3
  • Related: askubuntu.com/questions/463462/…
    – Ravexina
    Aug 11, 2020 at 8:20
  • 2
    Why should it reload automatically?
    – muru
    Aug 11, 2020 at 8:52
  • In addition to the others, you may want to learn that you can use a different file for your aliases. I use .bash_aliases, for example. I sometimes want to do different things with my aliases, so I also have a .bash_aliases2 and load that one as the source when I want to use that aliases profile.
    – KGIII
    Aug 11, 2020 at 19:15

1 Answer 1

8

.bashrc is read only once, when bash starts. It is just so by design (and has always been). If you make any subsequent changes to .bashrc, they won't be applied until .bashrc is re-read. By running source .bashrc, you make exactly this - you tell bash to re-read that file.

Of course you can also start a new copy of bash (by eg. starting a new terminal session), this will cause the new bash process read the .bashrc file again (but there will be no changes in the old session).

BTW. .bashrc file is a script, and by sourcing it, you execute the commands placed in that file. The commands define aliases in your case, but there can be virtually any commands placed in that file.

5
  • you could also use exec bash to replace the current bash process with a new.
    – user986805
    Aug 11, 2020 at 17:34
  • Just as an FYI, this would need to be root as the root user. Type sudo -s then run source ~/.bashrc
    – bikerben
    Oct 5, 2023 at 23:26
  • @bikerben Only if you want to change .bashrc file for root. If you want to change your own .bashrc file, there is no need to become root to reload it.
    – raj
    Oct 6, 2023 at 11:40
  • I suppose I should mention I needed to do this when using WSL. The method above didn't work otherwise.
    – bikerben
    Oct 8, 2023 at 15:00
  • Regardless of WSL or not, root privileges should not be needed to reload .bashrc of a non-root user.
    – raj
    Oct 8, 2023 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .