How can I write the following in a bash script?

tmux # Start tmux session.
compass watch /path/to/project1/compass/ # Run the first process.
Ctrl + B, " # Split the pane.
compass watch /path/to/project2/compass/ # Run the second process.
Ctrl + B, D # Exit the session.

3 Answers 3

tmux \
    new-session  'compass watch /path/to/project1/compass/' \; \
    split-window 'compass watch /path/to/project2/compass/' \; \

The new-session command (which creates a new tmux session) and the split-window command (which splits the current window into two panes) in tmux takes optional shell commands to run. The detach-client does the obvious at the end.

If you want a horizontal split (two panes side by side), use split-window -h in the command above.

When sending multiple tmux commands to tmux you need to separate them by ;. The ; needs to be protected from the shell by quoting/escaping it (';', ";" or \;), to stop the shell from interpreting it as the end of the tmux command.

I've split the whole thing into separate lines for readability. If you do this in a script (which I recommend), make sure there's nothing after the final \ on each line.

Reattach to the session with tmux a, tmux attach, or tmux attach-session (these are all equivalent).

The tmux session will end once both commands have finished executing.


This didn't work for me ( I was trying to do something like 'ls -la'). What did was:

tmux new-session -d bash
tmux split-window -h bash
#sends keys to first and second terminals
tmux send -t 0:0.0 "<my-first-command>" C-m
tmux send -t 0:0.1 "<my-second-command>" C-m
tmux -2 attach-session -d

This allowed me to run pretty generic stuff and although it looks ugly, it is pretty functional.

Just leaving it here in case other people are looking for the same thing.

Source: https://gist.github.com/kizzx2/4739236

  • 1
    Inspired by this answer and unix.stackexchange.com/a/409863/531914 : one need not assume session number and can use named sessions. E.g. tmux new-session -d -s foo; tmux send-keys -t foo.0 'C-c'; tmux send -t foo.0 'ls'; tmux send-keys -t foo.0 'Enter';. Also possible to extend this with splits or use of other named sessions at leisure.
    – cbugk
    Commented Jan 11 at 8:32

To run a short command without exiting at the end:

tmux \
    new-session  'ls ; bash' \; \
    split-window 'ls ; bash' 


tmux \
    new-session  'ls ; bash' \; \
    new-window   'ls ; bash' 

You must log in to answer this question.

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