tmuxtmuxExamples

Choose documentation 1

latest

tmux manual version

Latest (3.7c) 3.7c 3.2a
English

Edit this page on GitHub

Attach and send keys

A script can drive a shell it never shows. It starts a detached session, types into the pane with send-keys, and reads the screen back with capture-pane. The steps below add one command each. Every block is a range of lines from the finished send-command.sh, so each step shows code that runs. The complete script follows the last step. It needs tmux 3.3a or newer and a POSIX shell.

1. Choose a private socket

A tmux server listens on a socket, and a command reaches the server whose socket it names. Create a directory for this run and put the socket inside it, so the script never touches the tmux server you already use.

Terminal window
directory=$(mktemp -d "${TMPDIR:-/tmp}/libtmux-send-command.XXXXXX")
socket="$directory/tmux.sock"

mktemp -d makes a directory that no other run shares. Every later command repeats -S "$socket"; a command without it would reach your default server. Select a server covers the socket options.

2. Start a detached session

new-session -d creates a session without attaching your terminal to it. The first command to use a new -S path starts a server there, and -f /dev/null keeps that server from reading your tmux.conf. -s demo names the session, -n main names its window, and the final 'sh' is the program the pane runs.

Terminal window
tmux -S "$socket" -f /dev/null new-session -d -e PS1= -s demo -n main 'sh'

-e PS1= gives the pane an empty prompt, so a prompt cannot prefix the output that the script waits for in step 9. The command prints nothing and returns once the session exists; tmux.sock is now in the directory. To watch the pane yourself, pass the same socket to attach-session. The script reads the pane instead and never attaches.

3. Check the session by exact name

has-session exits with a non-zero status when no session matches, and set -e stops the script there. A bare -t demo matches every session whose name starts with demo. The = in =demo requires the exact name.

Terminal window
tmux -S "$socket" has-session -t '=demo'

4. Find the pane by its ID

list-panes prints one row for each pane in a window. -t '=demo:main' selects the window named main of the exact session. -f '#{pane_active}' keeps the rows whose pane is active, and -F '#{pane_id}' prints each pane’s ID in place of the default description.

Terminal window
panes=$(tmux -S "$socket" list-panes -t '=demo:main' \
-f '#{pane_active}' -F '#{pane_id}')

panes now holds an ID such as %0. Use the ID rather than the pane’s index: indexes move as panes come and go, and start at pane-base-index, while the ID names the same pane until it closes. Stable identity explains why.

5. Require exactly one pane

The filter should leave one row. set -- $panes splits the value into positional parameters, and [ "$#" -eq 1 ] fails the script when there are none or several. pane=$1 keeps the ID. IDs contain no whitespace, so the unquoted split is safe; the shellcheck comment records that choice.

Terminal window
# Pane IDs contain no whitespace; reject zero or multiple rows.
# shellcheck disable=SC2086
set -- $panes
[ "$#" -eq 1 ]
pane=$1

Require exactly one match covers the same check in the language libraries.

6. Type text without pressing keys

send-keys -l types its argument literally, character by character. The target -t "$pane" is the ID from step 4, so the text reaches that pane whichever window is current.

Terminal window
tmux -S "$socket" send-keys -l -t "$pane" "printf 'native input ready\\n'"

The typed text waits at the prompt. The shell has not run it, because no key has been pressed.

7. Press Enter

Without -l, send-keys treats an argument that names a key as that key. Send the Enter key to run the typed command. Key names such as Enter, C-c, and Up work this way. Adding -l here would type the word Enter instead.

Terminal window
tmux -S "$socket" send-keys -t "$pane" Enter

The shell now runs printf and prints native input ready on a new row. The sending keys guide also covers control keys and pane input modes.

8. Read the screen

capture-pane -p prints the pane’s visible text to standard output. Without -p, tmux copies the text into a paste buffer. The script redirects the output to screen.txt.

Terminal window
tmux -S "$socket" capture-pane -p -t "$pane" > "$directory/screen.txt"

A capture is a snapshot of what the pane shows at that moment, including the echoed command. Capturing output covers scrollback ranges and wrapped lines.

9. Poll until the line appears

send-keys returns when tmux has queued the input, not when the shell has run it, so the first capture can come too early. The loop repeats the capture, up to five times with a one-second pause, and stops as soon as grep -Fx finds the whole line native input ready. A whole-line match means the echoed printf command cannot satisfy the check, and the empty prompt from step 2 keeps a prompt from prefixing the line.

Terminal window
found=no
for attempt in 1 2 3 4 5; do
tmux -S "$socket" capture-pane -p -t "$pane" > "$directory/screen.txt"
if grep -Fx 'native input ready' "$directory/screen.txt" > /dev/null; then
found=yes
break
fi
if [ "$attempt" -lt 5 ]; then
sleep 1
fi
done

When a program can announce that it has finished, wait for that signal instead of a timer: wait-for blocks until another command signals a channel, and the capture example shows the polling alternative in more detail.

10. Report the result

If the line never appeared, the script prints an error to standard error and exits with status 1. Otherwise it prints the line, which is the script’s output.

Terminal window
if [ "$found" != yes ]; then
printf 'Expected pane output did not appear\n' >&2
exit 1
fi
printf 'native input ready\n'

11. Clean up

kill-server stops the private server and removes its socket. trap cleanup 0 runs cleanup whenever the script exits, whether it succeeded or failed, and the second trap turns HUP, INT, and TERM into a normal exit so cleanup still runs. If tmux cannot be stopped, the script keeps the directory and prints its path, so you can stop that server yourself. If another client closes the pane, the next command fails and the same trap retires the server.

Terminal window
cleanup() {
status=$?
trap - 0 HUP INT TERM
if [ -S "$socket" ] && ! tmux -S "$socket" kill-server; then
printf 'Cannot stop tmux; kept %s\n' "$directory" >&2
exit 1
fi
rm -rf "$directory" || status=$?
exit "$status"
}
trap cleanup 0
trap 'exit 1' HUP INT TERM

The complete script

The steps follow the order a reader meets the commands. In the file, cleanup comes first so that it covers every command after it. Save the script as send-command.sh:

send-command.sh
#!/bin/sh
set -eu
directory=$(mktemp -d "${TMPDIR:-/tmp}/libtmux-send-command.XXXXXX")
socket="$directory/tmux.sock"
cleanup() {
status=$?
trap - 0 HUP INT TERM
if [ -S "$socket" ] && ! tmux -S "$socket" kill-server; then
printf 'Cannot stop tmux; kept %s\n' "$directory" >&2
exit 1
fi
rm -rf "$directory" || status=$?
exit "$status"
}
trap cleanup 0
trap 'exit 1' HUP INT TERM
tmux -S "$socket" -f /dev/null new-session -d -e PS1= -s demo -n main 'sh'
tmux -S "$socket" has-session -t '=demo'
panes=$(tmux -S "$socket" list-panes -t '=demo:main' \
-f '#{pane_active}' -F '#{pane_id}')
# Pane IDs contain no whitespace; reject zero or multiple rows.
# shellcheck disable=SC2086
set -- $panes
[ "$#" -eq 1 ]
pane=$1
tmux -S "$socket" send-keys -l -t "$pane" "printf 'native input ready\\n'"
tmux -S "$socket" send-keys -t "$pane" Enter
found=no
for attempt in 1 2 3 4 5; do
tmux -S "$socket" capture-pane -p -t "$pane" > "$directory/screen.txt"
if grep -Fx 'native input ready' "$directory/screen.txt" > /dev/null; then
found=yes
break
fi
if [ "$attempt" -lt 5 ]; then
sleep 1
fi
done
if [ "$found" != yes ]; then
printf 'Expected pane output did not appear\n' >&2
exit 1
fi
printf 'native input ready\n'

Run the saved script:

Terminal window
$ sh send-command.sh

The output is native input ready.

The pinned tmux manual documents key names, literal input, and capture behavior.

Esc

Type to search.

Reference previewOpen reference