Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm following this tutorial for scripting the creation of tmux windows.

tmux new-window t $SESSION:0 -k -n MAIN 'cd ~/main/ && perl -Ilib myapp'

sometimes I kill myapp after I've started tmux, I want to drop back to a shell in that directory, but instead this kills the window entirely. What is the best way to fix this? I think at least one answer involves running a shell and having that shell run commands, but I'm not sure on the best way to accomplish that.

share|improve this question

2 Answers 2

tmux new-window -t $SESSION:0 -k -n MAIN 'cd ~/main/ && perl -Ilib myapp; $SHELL'

will launch your default shell when the previous commands have terminated (regardless of their return value, i.e. even if you didn't kill them).

If you only want to drop to the shell if the perl process was killed or failed (and have the tmux window close if it succeeded), use

tmux new-window -t $SESSION:0 -k -n MAIN 'cd ~/main/ && perl -Ilib myapp || $SHELL'
share|improve this answer

tmux already opens a shell for you in which it executes the cd and perl commands. If you don't want to run another shell under that, just so that you continue an interactive session (or prevent the window from closing, without using set-remain-on-exit), you can do:

tmux new-window -t $SESSION:0 -k -n MAIN
tmux send-keys 'cd ~/main/ && perl -Ilib myapp' C-m

If you want another shell under the tmux' created shell and want that shell to execute the commands and become interactive (which is different from @n.st answer that runs the shell afterwards), you can do:

tmux new-window -t $SESSION:0 -k -n MAIN 'bash --init-file <(echo "cd ~/main/ && perl -Ilib myapp")'

and if it is a problem that that bash has an incomplete environment do something like:

tmux new-window -t $SESSION:0 -k -n MAIN 'bash --init-file <(echo "source ~/.bashrc; cd ~/main/ && perl -Ilib myapp")'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.