0

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?

jaypal singh
  • 74,723
  • 23
  • 102
  • 147

2 Answers2

0

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • It gives an error when I use history -an. States I can only use one of the -anrw options at a time. I used history -a; history -n; instead. Still not getting what I would have expected. The goal is to have each spawned/forked subshell to inherit its parents history, not all shells. – user2471960 Jun 10 '13 at 19:05
  • Can you provide an example of how the parent's history is changing between spawned subshells? I'm not able to reproduce the problem. – unutbu Jun 10 '13 at 20:29
  • I moved that to the .bashrc, it still is not logging the commands to the .bash_history file until I terminate the shell or exit the subprocess. To test I clear the .bash_history file. Because the history is now essentially empty... there is no history to recall in the spawned subprocess, not even the parent shells history because it is not writing to the .bash_history – user2471960 Jun 11 '13 at 14:28
  • How about putting `PROMPT_COMMAND="history -a; history -n"` in `~/.bashrc`, then opening a new terminal. What does `echo $PROMPT_COMMAND` show? If you run `watch tail ~/.bashrc` and then open another terminal, and type some commands, do the commands appear in the `watch tail` terminal? – unutbu Jun 11 '13 at 14:34
  • The $PROMPT_COMMAND echos correctly. watch was not a command that bash recognized?! – user2471960 Jun 12 '13 at 02:03
  • So as I continue to investigate, the PROMPT_COMMAND is working. The issue that I am finding, the tool that spawns the subshell using os.system('bash') is not being logged. any command after that is logged in the .bash_history file, but not the commands that create subprocesses. – user2471960 Jun 12 '13 at 02:24
0

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

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • I am using the os.environ command to set environment variables for the child subshells. and that works great. Can this be used to propagate command history? – user2471960 Jun 11 '13 at 14:36