I haven't yet been able to find this among the Bash documentation so I was hoping it could get answered if I asked it here. Is there any way that I can, on execution of a script, branch its history (that is, to have the same history as the shell in the parent process which invokes the script, without using source or . to) and after completion, not have its history recorded in ~/.bash_history?

The purpose of not having its history recorded is so the last command before invoking the script only becomes the second last command after invoking the script. Is the easiest for this to just use set -o history, record the number of commands that get executed, and at the end of the script just delete that many lines from $HISTFILE?


Running a shell script starts a Bash process as a non-interactive shell. In this mode, history expansion doesn’t actually get carried out. From the Bash manual page, man bash:


The shell supports a history expansion feature that is similar to the history expansion in csh. This section describes what syntax features are available. This feature is enabled by default for interactive shells, and can be disabled using the +H option to the set builtin command (see SHELL BUILTIN COMMANDS below). Non-interactive shells do not perform history expansion by default.

I couldn’t see any mention of having a script save its commands to the Bash history file but I’ve never seen this happen in practice.

I’m assuming that you run your scripts in the standard way (as an executable file containing shell commands or by providing the script name as the argument to a bash command). If I’m not interpreting your question correctly, could you edit your question to clarify?

Your Answer

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

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