title: How to show a tmux environment variable in the status bar (as window format)
body: <p>What it comes down to is, I would like to </p>
  
  <ul>
  <li>print a variable unique to the window ,or</li>
  <li>run a script unique to the window</li>
  </ul>
  
  <p>and use the output in the status bar such that each window status is unique. I've tried to make it more clear through 2 scenarios:</p>
  
  <hr>
  
  <p><strong>Scenario 1</strong></p>
  
  <p>I'm trying to print a unique per window variable in the status bar. I've opened a shell inside tmux and have stored the tmux environment variable locally (per session) and globally (for all sessions) with the following commands, respectively:</p>
  
  <pre><code>bash&gt; tmux set-environment TMUX_STATUS_1 localvalue1
  bash&gt; tmux set-environment -g TMUX_STATUS_1 globalvalue1
  </code></pre>
  
  <p>I can verify these values for instance by going the another shell (in the same session) and typing: </p>
  
  <pre><code>bash&gt; tmux show-environment TMUX_STATUS_1
      TMUX_STATUS_1=localvalue1
  bash&gt; tmux show-environment -g TMUX_STATUS_1
      TMUX_STATUS_1=globalvalue1
  </code></pre>
  
  <p>I've tried to print the above value in the statusbar (both the local and global value) and have configured the window format as follows:</p>
  
  <pre><code>WINDOW='[#(tmux show-environment -g TMUX_STATUS_#I 2&gt;&amp;1)]'
  setw -g window-status-current-format $WINDOW
  setw -g window-status-format $WINDOW
  </code></pre>
  
  <p>Initially it only showed windows as '[]', after this I added the redirection of stderr to stdout and got the status bar showing the following:</p>
  
  <pre><code>[unknown variable: TMUX_STATUS_1] [unknown variable: TMUX_STATUS_2] [...
  </code></pre>
  
  <p>What needs to be changed to make the statusbar show (according to previous commands):</p>
  
  <pre><code>[globalvalue1] [unknown variable: TMUX_STATUS_2] [...
  </code></pre>
  
  <p>PS: it's not a status bar refresh issues, as i've used the following command after setting the variable to manually force a refresh of the statusbar:</p>
  
  <pre><code>tmux refresh-client -S
  </code></pre>
  
  <hr>
  
  <p><strong>Scenario 2</strong></p>
  
  <p>I've written a small bash script called 'tmuxscript' containing only:</p>
  
  <pre><code>echo "$(date '+%S') window:$(tmux display -p '#I') args:$@"
  </code></pre>
  
  <p>I updated my PATH variable and I have changed the window format to '[#(tmuxscript arg1 #I)]'. The output looks like:</p>
  
  <pre><code>[47 window:1 args:arg1] [47 window:1 args:arg1] [...
  </code></pre>
  
  <p>The time updates nicely. Printing the window index inside the script doesn't seem to work. The number 1 represents the window index of the currently focused window. This value is set for all windows, which is not what I want. I would at least expect to see (note the window index number) :</p>
  
  <pre><code>[47 window:1 args:arg1] [47 window:2 args:arg1] [...
  </code></pre>
  
  <p>Also, #I isn't getting past to the script, but the text 'arg1' is. How can I pas tmux variables to the script? </p>
  
  <hr>
  
  <p><strong>EDIT</strong>: I have now also tried setting the window status to:</p>
  
  <pre><code>'[#(tmux show-environment -g TMUX_STATUS_$\(tmux display -p "#I"\) 2&gt;&amp;1 | sed "s:^.*=::" )]'
  </code></pre>
  
  <p>Which gives me the following when the active (focused) window index is 1:</p>
  
  <pre><code>[globalvalue1] [globalvalue1] [...
  </code></pre>
  
  <p>Any help is appreciated!</p>