2

I know tmux run-shell can execute shell:

$ tmux run-shell "echo start; echo \${var}end"
start
end

But what if I want my shell could work interactively or read some user input,

$ tmux run-shell "echo start; read -p \"prompt:\" var; echo \${var}end"
start
end
  • Why read statement in above code doesn't work?
  • Could any tmux expert let me know ways to implement interactive action with tmux?

Background:

  • I put my various common work into a single kit.sh as functions all the time
  • There are many interactive questions in this script
  • Now I want to reuse this kit.sh with tmux hot key, as bind-key i "run-shell \"kit.sh\"" in my .tmux.conf
  • I'm planing to add some tmux send-keys in this kit.sh for kind of auto-type functions

Update 20231016:

My problem is solved as

bind-key i "new-window \"/path/to/my/kit.sh\""

And in kit.sh I can read user input interactively.

2
  • On runing tmux run-shell "read VAR" fails with read returning value 1. You might want to read github.com/tmux/tmux/issues/3187. It will be helpful if you could add what you are trying to achieve.
    – davidt930
    Oct 13, 2023 at 5:02
  • @davidt930 please check my update Oct 13, 2023 at 5:57

1 Answer 1

3

tmux run-shell subcommand does not work with programs that require tty. This explains why read fails when running in interactive mode[1] and also why

tmux run-shell "echo start; read -p \"prompt:\" var; echo \${var}end"

fails.

As you have already guessed one way is to use tmux send-keys[2]. You can also use split-window[3]:

tmux split-window "read -p prompt: VAR; echo \$VAR; read -p \"press any key\""

If you are using tmux version 3.2 or above you can also use display-popup subcommand[2]

tmux display-popup "read -p prompt: VAR; echo \$VAR"

[0] https://github.com/tmux/tmux/issues/3187#issuecomment-1127079855

[1] https://www.gnu.org/software/bash/manual/bash.html#index-read

[2] https://github.com/tmux/tmux/issues/3187

[3] https://github.com/tmux/tmux/issues/3617#issuecomment-1627569000

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .