1

I have configured my Gnome terminal on Linux Mint 18.3 to always start tmux by default by adding a couple of lines to my ~/.bashrc. This works extremely well, except that I also run another script through by adding it to my ~/.bashrc (one of those fancy custom welcome messages that show up when I open my terminal). As intended, this script is executed every time I open my terminal since it is defined below the execution of tmux in .bashrc.

The issue is that every time I subdivide a pane in tmux, or every time I create a new tab, this same script is executed again. Ideally, I would like to have it executed only when the Gnome terminal first opens (e.g. every time I would press Ctrl+Alt+T to open the terminal), but not when creating subsequent tabs and panes in an already opened terminal. What would be the best approach to accomplish this, since ~/.bashrc is executed at every new tab and pane?

I have come across a similar question here but that user wants their script executed only at the system startup. Also, their solution of creating a temporary file as a flag seems very hackish to me. Is there a better way of accomplishing what I want?

Edit: I am executing Tmux by adding the following lines to my .bashrc file:

# If not running interactively, do not do anything
[[ $- != *i* ]] && return
# Otherwise start tmux
[[ -z "$TMUX" ]] && exec tmux

Unfortunately, replacing the last one with

if [[ -z "$TMUX" ]]; then
    exec tmux
    my_script
fi

Does not work. In fact, even the test case below does not work; tmux is executed, but neither echo "A" nor echo "B" are:

if [[ -z "$TMUX" ]]; then
    echo "A"
    exec tmux
    echo "B"
fi
5
  • Does this answer help? You can detect if the shell is started within tmux or not, so that should allow you to choose which scripts to run. Another option is to configure the terminal to run some other command instead of just a plain shell (instead of useing bashrc for that), though I have no idea how to do that in Gnome terminal, as I don't use Gnome terminal.
    – dirkt
    Commented Sep 27, 2020 at 10:27
  • Thank you for your reply. I tried using the solutions discussed in that post but unfortunately it did not work out for me. I will try to research what could be done through gnome terminal, but I am puzzled as to why my simple .bashrc solution doesn't work. The main question for me is how to execute a script inside tmux immediately after exec tmux is summoned. (I made some edits in my question above) Commented Sep 27, 2020 at 15:27
  • Because exec replaces the current process with the called process, so after exec, nothing in the script gets executed. Also, even if you drop exec, nothing of that will get executed "inside" tmux.
    – dirkt
    Commented Sep 27, 2020 at 17:24
  • @dirkt Thanks for the info. Would you have any suggestion on how to tackle this then? Commented Sep 27, 2020 at 18:23
  • Everything that comes after [[ -z "$TMUX" ]] && exec tmux is execute inside tmux though, e.g. [[ -z "$TMUX" ]] && exec tmux; echo "inside tmux". But after the first line the $TMUX variable is set and so I can't use that as a conditional for the echo in this example (or script in my original case) Commented Sep 27, 2020 at 18:26

1 Answer 1

0

For tmux, use tmux set-env to set an environment variable that will be provided to all new panes in that tmux session.

if [[ $- == *i* && $TMUX && ! $_DID_GREET ]]; then
    ponysay "Hello!"
    tmux set-env _DID_GREET 1
fi

There is no equivalent for GNOME Terminal as far as I know (although it might be possible to poke through D-Bus to see whether the terminal window has more than one tab).

You must log in to answer this question.

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