Alexander Mills
Alexander Mills

Reputation: 100070

"Unsource" a .bash_profile or .bashrc script

I had a function in my ~/.bash_profile file, like so:

function foo {
  echo "foo"
}

after sourcing the .bash_profile file,

source ~/.bash_profile

I can run foo at the command line:

$ foo

say I want to change the name of foo to bar

function bar {
  echo "foo"
}

and I re-source the .bash_profile file

source ~/.bash_profile

It appears that both foo and bar are available at the command line. Why is that? and how can I clear the old .bash_profile code out of memory or wherever it's being stored?

Upvotes: 1

Views: 5132

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140186

the source command just merges the results of the executed file with the current environment. There's no reverting back to the previous state.

After such a change, most people close all existing windows and open new ones: problem solved;

But, you could delete all functions before sourcing your command (not only the ones contained in the sourced file, but all of them, which is maybe a bit overkill) like this:

unset `declare -F | cut -f3 -d" "`

(declare -F lists the functions, just get their names and pass them to unset).

Upvotes: 2

Dan Purdy
Dan Purdy

Reputation: 371

You need to unset foo

unset -f foo

By sourcing your bash profile you are essentially just 'adding' more information you aren't creating a fresh shell.

Upvotes: 1

Related Questions