Occasionally I throw together an interactive mode script using the shebang as such:

    #!/usr/bin/env bash -i

For some reason of recent, any scripts run in this mode have written their entire contents to my .bash_history file.

For example:

Contents of test.sh:

#!/usr/bin/env bash -i

echo "hello"
13:54:50 ツ :~ >./test.sh 

13:54:53 ツ :~ >history | tail -4

89969  #!/usr/bin/env bash -i
89970  echo "hello"
89971  ./test.sh 
89972  history | tail -4

If I don't have this line in my .bash_profile:

shopt -s histappend

Then the problem doesn't occur - but I lose my entire history each time I open a new Terminal tab.

All options in .bash_profile that relate to history are as follows:

shopt -s histappend
set +H
export HISTCONTROL=ignoreboth
export HISTSIZE=100000
export HISTFILESIZE=100000
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
  • Why do you run non-interactive scripts with an interactive shell? – Kusalananda Jan 22 '18 at 6:53
  • -i is for interactive shells. why run the script in an interactive shell when it's not? – cas Jan 22 '18 at 6:56
  • Mainly for testing and to alter trapping behaviour. Regardless, I used to be able to do this without the whole contents of the script being written to the history. – hmedia1 Jan 22 '18 at 7:24
  • IMHO, this is the correct behaviour. Typing the lines of the script into an interactive shell should save every line in the history. – Kusalananda Jan 22 '18 at 7:53
  • @Kusalananda I've thought about that. Perhaps if I was trying this for the first time and it did this, I would assume that to be the case, but I know for a fact it hasn't always been the case - also - why would histappend being off stop this from happening ? that option has nothing to do with the immediate history in the session, rather the history of that session as a whole. – hmedia1 Jan 22 '18 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.