57

I have sourced a script in bash source somescript.sh. Is it possible to undo this without restarting the terminal? Alternatively, is there a way to "reset" the shell to the settings it gets upon login without restarting?

EDIT: As suggested in one of the answers, my script sets some environment variables. Is there a way to reset to the default login environment?

6
  • 1
    I don't think "unsource" can be done in bash. You will have to create another script with bunch of unset commands to undo everything.
    – anubhava
    Commented Jan 6, 2012 at 15:58
  • is your problem primarily that you have overwritten your TERM variable and your terminal session has gone wonky? If so, it might help to edit your question to make that clear. Good luck.
    – shellter
    Commented Jan 6, 2012 at 16:04
  • I've submitted an edit to this question to change 'terminal' to 'shell'. You want to reset your shell (bash), not the terminal (e.g. xterm) in which the shell is running. Is that right? Commented Jan 6, 2012 at 16:04
  • @Benjamin, can you give an example of some of the things you wish to undo? Environment variables and aliases, that sort of thing? I want to be sure that you only wish to reset the shell, and not to reset the terminal (such as the window title of the terminal). Commented Jan 6, 2012 at 16:07
  • 1
    I still don't see a conclusive answer... I want to return to the same environment as when I first login, without closing and opening a new shell.
    – Benjamin
    Commented Jan 6, 2012 at 21:24

7 Answers 7

45

It is typically sufficient to simply re-exec a shell:

$ exec bash

This is not guaranteed to undo anything (sourcing the script may remove files, or execute any arbitrary command), but if your setup scripts are well written you will get a relatively clean environment. You can also try:

$ su - $(whoami)

Note that both of these solutions assume that you are talking about resetting your current shell, and not your terminal as (mis?)stated in the question. If you want to reset the terminal, try

$ reset
1
  • 2
    I would even add exec to avoid subshells: exec su - $(whoami). Entering a password (asked by su) is kind of annoying though.
    – pawamoy
    Commented Oct 23, 2017 at 17:29
16

No. Sourcing a script executes the commands contained therein. There is no guarantee that the script doesn't do things that can't be undone (like remove files or whatever).

If the script only sets some variables and/or runs some harmless commands, then you can "undo" its action by unsetting the same variables, but even then the script might have replaced variables that already had values before with new ones, and to undo it you'd have to remember what the old values were.

If you source a script that sets some variables for your environment but you want this to be undoable, I suggest you start a new (sub)shell first and source the script in the subshell. Then to reset the environment to what it was before, just exit the subshell.

10

The best option seems to be to use unset to unset the environment variables that sourcing produces. Adding OLD_PATH=$PATH; export OLD_PATH to the .bashrc profile saves a backup of the login path in case one needs to revert the $PATH.

9

Not the most elegant solution, but this appears to do what you want:

exec $SHELL -l
2
  • This solved my problem but it quits the program running the command. It would be useful to have some understanding of what this does.
    – kilojoules
    Commented Dec 3, 2017 at 17:17
  • Be aware of this doesn't un-export the alias/function, e.g. the $PATH will keep growth with duplicated paths. Need special function to add the PATH.
    – 林果皞
    Commented Aug 15, 2019 at 5:33
8

My favorite approach for this would be to use a subshell within () parantheses

#!/bin/bash
(
  source some_script.sh
  #do something
)
# the environment before starting previous subshell should be restored here
# ...
(
  source other_script.sh
  #do something else
)
# the environment before starting previous subshell should be restored here

see also

https://unix.stackexchange.com/questions/138463/do-parentheses-really-put-the-command-in-a-subshell

1

I don't think undo of executed commands is possible in bash. You can try tset, reset for terminal initialization.

4
  • This just clears the screen and so on. However, the OP wants to reset the shell (bash), not the terminal. Commented Jan 6, 2012 at 16:02
  • On second thoughts, I'm not so sure what the OP wants! Maybe there are terminal-specific settings (such as the window title) that the OP wishes to reset. I shouldn't have downvoted so soon! Commented Jan 6, 2012 at 16:08
  • You are correct, I want to reset the shell without closing it.
    – Benjamin
    Commented Jan 6, 2012 at 16:11
  • You could run 'bash' again inside the existing shell to get a new initialized shell.
    – rahool
    Commented Jan 6, 2012 at 16:15
1

Depending what you're sourcing, you can make this script source/unsource itself.

#!/bin/bash

if [ "$IS_SOURCED" == true ] ; then
  unset -f foo
  export IS_SOURCED==false
else
  foo () { echo bar ; }
  export IS_SOURCED==true
fi

Your Answer

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

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