TransWikia.com

Executing script only on terminal startup with tmux

Super User Asked by gilbertohasnofb on January 27, 2021

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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP