2

Assume I run a tmux session on machine A and from it I start an interactive shell via ssh to machine B.

I'd like to change machine B's PS1 to better suit the use from under tmux; for instance, I'd include different things into the XTerm title string.

I would rather not edit machine B's settings to always show a tmux-optimized prompt. Sometimes I ssh to machine B from a plain terminal (not tmux) and would like it to use my regular PS1 in that case.

Is there a standard / accepted best way to achieve this?

All I came up with so far is setting AcceptEnviron in machine B's sshd config and pass a custom signal variable at ssh connection time.

1

This is one possible way to do what you are looking for:

tmux sets environment variables in the shells it creates (example $TMUX, $TMUX_PANE), its possible to detect these and run your ssh command to inform your remote session how client has started, we are looking at two steps.

First, detecting where ssh is being started, this can be done in a function, named 'ssh' for the sake of this example.

function ssh {
    if [ -z "$TMUX" ]; then
        # echo connecting from a plain terminal
        /usr/bin/ssh $1 # $1 is <user@hostname>
    else
        # echo connecting through tmux
        /usr/bin/ssh -t $1 bash --rcfile '~/.bashrc.tmux'
    fi
}   

Note : functions take priority when named the same as programs in your path, name the function differently if you would like to avoid confusion.

Second, to let your remote host set your prompt accordingly, create a file ~/.bashrc.tmux on your remote host, like

# contents of .bashrc.tmux, override PS1 to your liking
source .bashrc
PS1="[\d \t \u@\h:\w ] $ "

alternately, you could set an environment variable to be used in more places

RUNNING_THRU_TMUX=true
source .bashrc
  • Nice, I'll give it a try! – 9000 Jun 28 '15 at 4:36

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.