GNU Bash version 5.1, WSL, Ubuntu 22.04.3 LTS.
$ bash -version
bash -version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
- Edit
.bashrc
, create functionfoo
:
foo() {
echo 'GHOST'
}
save and exit
- Source
.bashrc
:
. .bashrc
Now foo
is a valid function:
type foo
foo is a function
foo ()
{
echo 'GHOST'
}
Call
foo()
, it will printGHOST
Edit
.bashrc
, remove the function, source againfoo
as a function still available for calling andtype
.Restart a terminal window, it now stops existing.
Of course it also happens when you rename a function; and you keep calling the old version while repeating it pressing UP to use the one in history.
Bug? Feature? Exploit?
Neither a bug nor a feature nor an exploit but simply a logical result of how things work. Sourcing
.bashrc
isn't anything special or magic. It doesn't re-create your shell setup or reset your session. It is no different to sourcing any other file,.bashrc
is just the default name for the shell initialization file but even that can be changed by usingbash --rcfile whatever
or by settingBASH_ENV
for non-interactive shells.So when you source
.bashrc
, that doesn't remove anything, it simply adds to what you already have. It's like reading a book: after you ahve read one book, reading another book doesn't remove what you have learned in the first one, it simply adds to it. The only real difference is that in the case of bash, anything in the new sourced file that contradicts something in the old will overwrite the old. If, therefore, instead of deletingfoo()
you added a differentfoo()
function, then this new one would overwrite the old one.This isn't any form of caching, by the way. You just have a running process and tell it to import new stuff. The new stuff gets imported, anything that has the same name in the new stuff will be reset to the new values, anything not mentioned in the new stuff won't be touched.