I'm writing a script. I really want to know this. I hope tmux executable can tell me this.

I find

tmux rename <newname>

can rename current session. But I can't find a command to fetch its own name.

5 Answers 5


With tmux 1.2 (and later), you can use the -p option of display-message to output a message to stdout (instead of displaying it to an attached client):

tmux display-message -p '#S'

#S is formatted as the session name (see the description of the status-left option in the man page).

  • +1, much better than my answer.
    – chepner
    Apr 9, 2012 at 12:52
  • I really appreciate that you point out it require tmux 1.2, which is exactly the version available for openSUSE 11.3 auto online update.
    – Jimm Chen
    Apr 10, 2012 at 1:19
  • 1
    With tmux 1.8, this returns the name of an attached session even when I'm not working in that session. Aug 11, 2016 at 3:44
  • Tim People's answer below works with tmux 1.8 and later, as well as addresses the points he raises. That is the better answer.
    – Alexandros
    Nov 26, 2018 at 11:52

I'm surprised that, after nearly 5 years, no one has pointed out that neither of these answers is adequate. While both work fine as long as the current TTY is attached to the only tmux session on the host, these answers both fall flat if:

  1. the current terminal session is not part of a tmux session, or
  2. there are multiple, attached tmux sessions

In the former case, both answers here report back the name of the attached session (regardless of whether the current tty is governed by that session). In the latter case, the result is likely indeterminate or will result in multiple answers.

The proper question should be, "What is the name of the tmux session to which my current terminal session is attached?"

For he answer to that question, execute:

for s in $(tmux list-sessions -F '#{session_name}'); do
    tmux list-panes -F '#{pane_tty} #{session_name}' -t "$s"
done | grep "$(tty)" | awk '{print $2}'

This works regardless of the number of tmux sessions (attached or otherwise) and regardless of whether the current terminal session is or is not part of a tmux session.

  • 3
    Great answer. Rather than looping through each session name to list its panes, you can also use tmux list-panes -a -F '#{pane_tty} #{session_name}' to list every session's panes. Not sure how new -a is, so YMMV. Jun 3, 2017 at 22:12
  • 1
    Came back to this question after using accepted answer and failing as per issues described here. Please upvote this. If possible the accepted answer should be changed.
    – Alexandros
    Nov 26, 2018 at 11:39
  • 4
    Method 1 works fine if combined with checking TMUX environment variable prior to running tmux display-message -p '#S' for a typical desktop setup.
    – ZeroPhase
    Apr 28, 2019 at 8:50
  • tmux 3.2 added -f filters which will let you shorted this to a single command. Not sure what version added #{==:...} formats that let you turn -F output conditional on a match to arbitrary strings, but that can be used to only output the session name for the current tty as well, but then you have to post-process to remove the blank lines. Jun 17, 2022 at 20:54
  • As I am on 3.1c I use tmux list-panes -a -F "#{?#{==:#{pane_tty},$(tty)},#{session_name},}" | grep '.'. That's #{?<test>,<true>,<false>} on the outside to conditionally change the output, the test uses #{==:<value1>,<value2>} to test if two strings are equal, testing #{pane_tty} against $(tty) (interpolating the current tty string), and if that's true, #{session_name} is output, otherwise nothing. Jun 17, 2022 at 21:00

Extended/corrected tim-peoples’s answer as per don_crissti’s comment on Why is this grep -v not functioning as expected?.

"$(tty)" command in tim-peoples’s answer

| grep "$(tty)" |

would not work in that context as expected. It evaluates to a string 'not a tty'.

Replacing it with a variable solves this problem.



| grep "$tty" |

Also, when no tmux sessions exist, the original code would produce

"no server running on /tmp/tmux-1000/default" error message.

Add 2>/dev/null and the code would run without printing out the error message.

Modified code reads as:

for s in $(tmux list-sessions -F '#{session_name}' 2>/dev/null); do
    tmux list-panes -F '#{pane_tty} #{session_name}' -t "$s"
done | grep "$tty" | awk '{print $2}'
  • 1
    What's the improvement? If it's a necessary one you should consider submitting it as an edit rather than an answer. To make it a good answer you probably should consider including some more descriptive text.
    – Seth
    Apr 24, 2017 at 12:21
  • 1
    @seth, I wanted to comment but oddly didn't have enough credit to do so. Apr 24, 2017 at 13:52
  • You need 50 reputation to comment. It's usually not a good idea to comment with complex code as you lack line breaks and so on. Your edit made it a pretty good answer IMHO.
    – Seth
    Apr 25, 2017 at 5:12
  • 2
    @TimPeoples While it might be a Bash issue. Bash is still the standard to code shell scripts against for compatibility on modern terminals. Sure, the posix standard is ideal; but, bash has some features all of the more advanced terminals share. By hitting Bash most of the other major terminals should just work. I just run shell check and catch a bunch of bad errors and formatting issues. (mostly using old syntax in place of current more descriptive syntax)
    – ZeroPhase
    Apr 28, 2019 at 6:45
  • Chris Johnsen's answer returns a session name even when querying from outside of one.
  • bright side's and Tim Peoples' answer assume a tty.

Instead, I found this to work accurately for me:

if [[ -n "$TMUX_PANE" ]]; then
    session_name=$(tmux list-panes -t "$TMUX_PANE" -F '#S' | head -n1)
  • I don't think you need the head line since there should be only one pane with the TMUX_PANE pane id I believe?
    – studgeek
    Jan 24, 2021 at 21:12
  • @studgeek IIRC that is to address the first bullet point - we do not attempt to resolve session_name if $TMUX_PANE is not even set.
    – dset0x
    Feb 12, 2021 at 17:28
  • Yes, agree the TMUX_PANE check makes sense. What I'm suggesting that the | head -n1 shouldn't be needed since only one row (or no rows) can be returned.
    – studgeek
    Feb 14, 2021 at 17:16
  • 1
    @studgeek list-panes prints a row for every pane in your tab (try making a split and then run the command)
    – dset0x
    Feb 20, 2021 at 16:35

Not ideal, but you can extract the name of the attached session with awk:

tmux list-sessions -F '#{session_name} #{session_attached,yes,}' | awk '$2=="yes" {print $1}'

The custom format is a little easier to parse than the default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.