2

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

5

.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.

1
  • you could also use exec bash to replace the current bash process with a new.
    – user986805
    Aug 11, 2020 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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