4

I would like to have a command or script that opens a new gnome-terminal window with bash and loads a history file and stays open.

I searched for how to open a terminal and execute a command without it closing, and the answers I found are essentially summarized in this stackoverflow answer.

Based on these methods, I tried running the following command:

gnome-terminal -e "bash -c 'history -r ~/history.txt; exec $SHELL'"

However, the history that appears in the new window is not the one in history.txt but the one in the default file, .bash_history. I thought since after the history -r command I am executing bash, and that sources the .bashrc file, maybe .bashrc is doing something that affects the history I loaded.

Based on that, I tried the rcfile option in the answer linked above, so that I can include commands after .bashrc, resulting in the following rcfile:

FILE=~/.bashrc && test -f $FILE && source $FILE
history -c && history -r ~/history.txt

However, when I run the following command:

gnome-terminal -e "bash --rcfile rcfile"

The history in the new terminal is still the one from .bash_history. If I add the history command to the rcfile it shows that the history from history.txt was loaded, so something after the rcfile is overwriting the history I load.

I also found that if I unset the HISTFILE variable with export HISTFILE='' at the end of the rcfile, the history from history.txt is not overwritten and works. However, I do not want to unset HISTFILE because I do want the history to be saved to .bash_history when I use the newly opened terminal.

Finally, I found the -o option in bash, so I tried doing what I want with that. I changed my rcfile to:

FILE=~/.bashrc && test -f $FILE && source $FILE
history -c && history -r ~/saved/history.txt
set -o history

and ran the following command:

bash --rcfile rcfile.txt +o history

I was hoping that running bash with +o history would prevent the history from history.txt from being overwritten, while the set -o history command in the rcfile would reactivate the bash history. However, this also does not work as intended, as the history is the one from history.txt but then there is no more history logging and I have to manually enter set -o history for it to work.

So basically, everything I tried brings me back to the same question: how do I open the new terminal with bash and then execute a command, as if a user were entering it?

Additionally, I would like a better understanding of what is happening after the rcfile is executed, as I wasn't able to find any other relevant file that was being sourced. And is there a way of disabling that behavior from the command line?

1

I managed to find a workaround using the PROMPT_COMMAND variable and the rcfile.txt file mentioned in the question. You can run the command

gnome-terminal -e "bash --rcfile rcfile.txt"

Where the contents of rcfile.txt are the following:

FILE=~/.bashrc && test -f $FILE && source $FILE

my_init_func() {
    # Insert desired commands here. In my case, history -r history.txt
    PROMPT_COMMAND=`echo $1`
    eval $PROMPT_COMMAND
    unset -f $FUNCNAME
}

PROMPT_COMMAND="my_init_func \'$PROMPT_COMMAND\'"

What this does is source the default .bashrc if it exists in order to keep any other terminal configurations. Then, it defines a function that will run once when calling the PROMPT_COMMAND used in bash, then will unset itself and restore the original PROMPT_COMMAND.

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.