Ubuntu – Setting Terminal tab titles

gnome-terminal

I'm trying to open up a few terminal tabs in gnome-terminal, and so far I've managed to do something, but I'm stuck now.

So, I have the following requirement:

  • Open tabs titled "X" and "Y"
  • Execute some commands
  • Keep the tabs open and ready for further use; keep the title.

So far, I managed to meet some of the requirements, but not all of them:

gnome-terminal --tab -t "X" -e "bash" --tab -t "Y" -e "top"

This opens two tabs:

  1. "X" (and then changes the title to the default title)
  2. "Y", but the tab closes as soon as I quit top.

Is there a way to open up a tab, launch bash, but not change the title? I've tried Google, but gave up.

EDIT: It doesn't has to be a command.

Best Answer

As you can see in other answers, the title of the tab is changed by the shell every time it outputs a prompt. And after executing top your tab exits because the command you told it to run finishes...

I'll do the following:

Step 1: call the terminal with shells, adding environment variables like that:

gnome-terminal --tab -t X -e "env MYTAB=X bash" --tab -t Y -e "env MYTAB=Y bash" 

Step 2: add at the end of your .bashrc the following code:

#if MYTAB is not set, return
[ -z "$MYTAB" ] && return
# reset the cursor and title 
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
PS1="\[\e]0;$MYTAB \w\a\]$PS1"   #title: $MYTAB and current dir
# execute the commands for every tab
case "$MYTAB" in
        X)
                echo this is X
        ;;

        Y)
                echo this is Y 
                top
        ;;
esac

...which I think is easy to understand and you can modify with the command/tweaks you need. Tested and works ok; after you exit top from the tab you'll still have the prompt and the tab for you to peruse.

Screenshot (after pressing "q" in top):

screenshot