2

I have the simple following terminal.sh script:

#bin/bash --login
str=(--tab --title="Test" -e "bash --login -c 'echo history';bash")
gnome-terminal "${str[@]}"
exit 0

I basically use it to open a terminal with several tabs that I need to use daily. But the problem is that the echo history command is not added to the history of that tab. So, for example, one of my tabs is the application server. When I need to restart it, I usually type Ctrl+C, Ctrl+L, Up and Enter, but when I use this script to run it, it doesn't add the line executed to the bash, and then the last command executed is normally exit. So I end up closing the tab instead of restarting the server, which is incredbly annoying.

I saw this, and I tried adding:

HISTFILE=~/.bash_history
set -o history

To the start of my terminal.sh script, and also tried adding to the script executed:

str=(--tab --title="Test" -e "bash --login -c 'HISTFILE=~/.bash_history;set -o history;echo a';bash")

But neither helped. I also tried adding history -s echo history before executing the script, but that didn't help either.

How can I add all commands executed this way to the history?

I aslo tried:

#bin/bash --login

cmd='echo test'
cmd_e="bash --login -c 'unset PROMPT_COMMAND;history -s '\''$cmd'\''; eval '\''$cmd'\''';bash"
str=(--tab --title="History Test" -e "$cmd_e")

gnome-terminal "${str[@]}"

exit 0
1

1 Answer 1

3

You could do something like:

SHELL=/bin/bash PROMPT_COMMAND='unset PROMPT_COMMAND; history -s "$CMD";
    eval "$CMD"' CMD='vi /etc/issue' gnome-terminal

(here using vi /etc/issue as your command).

That has the advantage of running $CMD as a job of that shell, so you can suspend resume, interrupt it as other jobs and it can affect the environment of the shell (for instance you can do CMD='PATH=$foo:$PATH').

It assumes your ~/.bashrc doesn't set $PROMPT_COMMAND itself.

Edit:

For multiple tabs:

PROMPT_COMMAND='unset PROMPT_COMMAND; history -s "$CMD"; eval "$CMD"
  ' gnome-terminal --tab --title /etc/issue -e 'env CMD="vi /etc/issue" bash' \
                   --tab --title who -e 'env CMD=who bash'
1
  • Your code work flawlessly the way it is! But I'm having trouble integrating it with mine... I need my syntax because I need to open multiple tabs in the same terminal, each one with one title. I added how I did it on the question. It runs, echoes, but doesn't add...
    – Luan Nico
    May 21, 2014 at 13:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .