问题
I have written a series of python tools that spawn new bash sessions. I am want those individual subshells to inherit the command history of the parent. I have tried:
shopt -s histappend
PROMPT_COMMAND="history -an;$PROMPT_COMMAND"
in the .bash_profile
. But it didn't give me what I need. I have seen this done. My tool uses:
os.system('bash')
to spawn a subprocess.
Thoughts? better way?
回答1:
site.py sets os.environ at startup. This does not change unless you explicitly change os.environ. So, a call to os.system('/bin/bash') should have an environment that was the same as you got as python startup.
Check os.environ, right after startup, and if necessary make changes to os.environ. Directly.
python docs os
回答2:
Depending on your OS, .bash_profile
may only be executed by login shells, while .bashrc
is executed for non-login shells.
So try putting
shopt -s histappend
PROMPT_COMMAND="history -a; history -n"
in .bashrc
instead of .bash_profile
.
来源:https://stackoverflow.com/questions/17030034/unix-subshell-inheriting-the-command-history-from-parent-bash