0

I am on macOS and using zsh. I have created a folder ~/bin/ which I've added to my path. I created a shell script ~/bin/testscript containing the following code

#!/bin/sh
tmux new-session -s testsession
tmux split-window -v

and used chmod -R 700 ~/bin to set the permissions -rwx------.

When executing the lines from the shell script successively by typing them in the shell I get a new tmux session with vertically split windows. This is the expected behavior. However, when I type testscript into the shell I only get a new tmux session, but without vertically split windows.

What do I need to do to change in the script to get a session with split windows? I've copied the above code from this thread, but it doesn't work for me.

  • If you type `test` in the shell and your script actually runs at all, then there is something you're not telling us. `test` should be a shell builtin. What mechanism are you using to avoid that? – William Pursell Apr 16 '21 at 15:50
  • I didn't tell you that my script is actually named 'testscript' instead of 'test'. I edited my post. – user9007131 Apr 16 '21 at 15:58

2 Answers2

2

When you run those command manually, you are typing the 2nd line in a shell running in the new session. When you run the script, the 2nd line is not executing inside that session. Try:

tmux new-session -s testsession "$SHELL" \; split-window -v

or

#!/bin/bash

tmux new-session -d -s testsession
tmux split-window -v -t testsession:0.0
tmux attach-session -t testsession
William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

You get a new session, and you immediately attach to that session. This causes the first call to tmux to block until you detach from that session, preventing the script from preceding to the second call to tmux.

To prevent that, create the new session without attaching to it immediately by using the -d option. Once you've used split-window to create the second pane, then you can attach to the session.

#!/bin/sh
tmux new-session -d -s testsession
tmux split-window -v -t testsession
tmux attach -t testsession

split-window's -t option technically takes a window, not a session, but as the new session only has one window, it defaults to that window.

tmux attach, with no -t option, will attach to the most recently used session, so -t testsession can be considered optional here.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 'This causes the first call to 'tmux' to block.' I think my mental model is wrong. Isn't the first call to tmux 'tmux new-session -s testsession' already fully executed once the new session is created, and I immediately attached to it? In my imagination that would mean that the second line, 'tmux split-window -v' is then executed _within_ the new tmux session exactly _because_ I've immediately attached to it. – user9007131 Apr 16 '21 at 16:10
  • 1
    `tmux` is heavily overloaded; it's an executable that could create a `tmux` session or server or client, depending on its arguments. In the case of `new-session`, it creates a new session, then starts a client *attached* to that session; the process itself doesn't exit until the client detaches from the session. With `-d`, no new client is created; only a new session, allowing the `tmux` process to exit immediately after creating the session. `tmux attach` simply creates a new client that attaches to a pre-existing session. – chepner Apr 16 '21 at 16:26