Search code examples
awkescapingtmuxgawkquoting

Quoting a complex awk program in tmux.conf


A user on Freenode #tmux asked:

  1. How can we properly escape this shell command using GNU awk for set -g tmux status-right?

    sensors | awk '/^Physical id 0:/ { s = $4; sub(/^+/, "", s); print s; exit }'
    

    The result should be 45.0°C.

  2. Also, how can we make it update every 30 seconds?

The output of sensors:

coretemp-isa-0000
Adapter: ISA adapter
Physical id 0:  +45.0°C  (high = +80.0°C, crit = +100.0°C)
...

Solution

  • Setting status-right

    Quoting with shell command #( ) in tmux

    Quoting is complex in tmux #( ) because the contents are evaluated twice.

    For this reason let's simplify the gawk program to:

    sensors | awk '/^Physical id 0:/ { sub(/^+/, "", $4); print $4; exit }'
    

    Now we plug it into .tmux.conf:

    set-option -g status-right "#( sensors | awk \\' /Physical id 0:/ {  sub\\(/\+/,\"\",$4\\); print \$4; exit }  \\')"
    

    But that's terribly complex to read and change next time you go tinkering...

    An easier alternative

    The easiest solution is to put the shell command into a file and call it from tmux.

    ~/bin/tmux-status.bash:

    #!/bin/bash
    sensors | awk '/^Physical id 0:/ { sub(/^+/, "", $4); print $4; exit }'
    

    ~/.tmux.conf:

    set-option -g status-right "#(bash ~/bin/tmux-status.bash)"
    

    Make it update every 30 seconds

    set-option -g status-interval 30
    

      

    See also