是否可以在 bash 中“取消源代码”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8760505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to "unsource" in bash?
提问by Benjamin
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?
我在 bash 中找到了一个脚本source somescript.sh
。是否可以在不重新启动终端的情况下撤消此操作?或者,有没有办法在不重新启动的情况下将外壳“重置”到它在登录时获得的设置?
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?
编辑:正如其中一个答案中所建议的,我的脚本设置了一些环境变量。有没有办法重置为默认登录环境?
采纳答案by Benjamin
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
.
最好的选择似乎是用来unset
取消设置采购产生的环境变量。添加OLD_PATH=$PATH; export OLD_PATH
到.bashrc
配置文件会保存登录路径的备份,以防需要将$PATH
.
回答by William Pursell
It is typically sufficient to simply re-exec a shell:
简单地重新执行一个 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
请注意,这两个解决方案都假设您正在谈论重置当前的 shell,而不是您在问题中(错误?)陈述的终端。如果要重置终端,请尝试
$ reset
回答by Celada
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).
否。采购脚本会执行其中包含的命令。无法保证脚本不会执行无法撤消的操作(例如删除文件或其他任何操作)。
Ifthe 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.
如果您为您的环境设置了一些变量的脚本,但您希望它可以撤消,我建议您首先启动一个新的(子)shell,然后在子shell 中获取脚本。然后要将环境重置为之前的状态,只需退出子shell。
回答by Corey Henderson
Not the most elegant solution, but this appears to do what you want:
不是最优雅的解决方案,但这似乎可以满足您的需求:
exec $SHELL -l
回答by rahool
I don't think undo of executed commands is possible in bash. You can try tset, reset
for terminal initialization.
我认为在 bash 中无法撤消已执行的命令。您可以尝试tset, reset
进行终端初始化。