I want to use a script to open a tmux session with 6 windows, each in a different directory. I started with a script I found and tried this first:
tmux new-session -s xyz -n etc -d 'cd /etc'
tmux new-window -t xyz:1 -n var 'cd /var/log'
But I soon found out that this will not work as I expected — the window will be closed after the shell command completes.
So my next idea was to start a new shell like this:
tmux new-session -s xyz -n etc -d 'cd /etc; bash -i'
tmux new-window -t xyz:1 -n var 'cd /var/log; bash -i'
tmux new-window -t xyz:2 -n var2 'cd /var/log; bash -i'
tmux new-window -t xyz:3 -n var3 'cd /var/log; bash -i'
tmux new-window -t xyz:4 -n var4 'cd /var/log; bash -i'
tmux new-window -t xyz:5 -n var5 'cd /var/log; bash -i'
tmux new-window -t xyz:6 -n var6 'cd /var/log; bash -i'
tmux select-window -t xyz:1
tmux -2 attach-session -t xyz
This almost works. But if I start more than about 4 windows, I frequently see the following bash errors in one of the windows after startup:
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: too many arguments
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
bash: [: =: unary operator expected
I have no idea why this happens, but I still think that I’m not doing this right. Is there a better way to set up a tmux session over several directories?
The shell errors are probably due to some problem in your startup files (or something they run).
As shellter commented, temporarily including the command
set -vxearly in your startup sequence is a good way to find out where the errors are occurring.If you find the
-vxoutput too verbose, you could try “printf debugging” (manually adding debug statements to your startup files until you can narrow down exactly which lines are causing the errors):echo start of .bashrcandecho end of .bashrcat the start/end of your.bashrcto see if the error occurs during your.bashrc. If not, instrument your other startup files:.bash_profile/.bash_login/.profile. If the errors happen before that file, then the problem may be in/etc/profile.Note: These debug additions need to be temporary since they will cause problems if you ever use a program that makes automated logins (e.g. rsync, SSH-based Git access, etc.) since these programs expect a “clean” connection without such debugging noise present.
There should be no need to use
cdcommand like that in the shell-command argument given to eithertmux new-sessionortmux new-window.A new window will “inherit”† the current working directory when using
new-sessionandnew-windowfrom the command line (i.e. when done through thetmuxbinary, instead of via a binding or at a tmux–:prompt). According to the CHANGES file, it looks like this has been the case since tmux 0.6 (at least fornew-window).† This is tmux-mediated inheritance, not the parent–child inheritance that is the usual mechanism for passing along the cwd.
This script works for me with tmux 1.5:
This might also (as a side-effect) alleviate your shell startup errors since the way tmux starts a shell is different from a plain
bash -i(it is more akin tobash -l, which uses your.bash_profile/.bash_login/.profileinstead of (just) your.bashrc).