tmux does not inherit $SHELL variable from shell launching tmux - shell

After updating my /etc/shell file and typing a csh -s /usr/local/bin/zsh afterwards, I decided to quit and relaunch tmux with hope it will take account of my new $SHELL environment variable. Unfortunately, tmux still shows the old /bin/zsh after typing echo $SHELL.
How to tell tmux to inherit the $SHELL variable of the shell that launched it?

set-option -g default-shell "$SHELL_PATH"
in ~/.tmux.conf or /etc/tmux.conf. Works on arch.
You can reload the config file with : source-file e.g.
<C-b>: source-file ~/.tmux.conf
You would need to do it for every tmux instance. Otherwise you may restart
tmux with killall tmux; tmux
these commands will change the path of shell in tmux

Related

Why tmux fails to run a bash script when using a bind-key

The script using the normal command ./<script.sh> in a terminal window works without any problem. It troughs error returned 127 when running a bash script with bind-key:
tmux version 3.3a
bind-key -r i run-shell "tmux neww \<script path\>"
I have also tried the run the script from tmux command prompt using the :shell-run 'path/script/script.sh command it still does nothing.
Could someone explain what could be the issue. I was wondering if it could be related to tmux's default sh shell. The script uses #!/usr/bin/bash and not #!/usr/bin/sh.
The run:shell command still fails when I the script to #!/usr/bin/sh

How can I use bash variables in .tmux.conf?

I'm trying to write my own .tmux.config file and have managed to get most of it working (the file is quite simple). The intention is to have a minicom session running on the right side, whilst simultaneously saving the communication into a file. There is one problem. I define a UART_LOGFILE_NAME variable in my .bashrc file and export it, however, tmux still returns invalid environment variable. Is there something I'm missing?
my .tmux.conf file looks somewhat like this:
new-session -s serial -n console
split-window -h -l 30%
send-keys "sudo minicom rasp.serial -C ${UART_LOGFILE_NAME}"
split-window -v -l 10%
I can't find anything relevant in tmux manual entry except for the GLOBAL AND SESSION ENVIRONMENT. Also, sice I'm using the send-keys command I thought that the .bash_profile and therefore .bashrc files have been sourced before the minicom is launched (I have already managed to get the minicom running without logging and all panes have the UART_LOGFILE_NAME variable correctly defined after when I run tmux without logging of minicom session).
Thank you for your help

How to pbpaste in .bash_profile alias?

I try to make a small alias script to automatically reset my OS X Fuse connection.
If I run this command in my terminal:
pgrep sshfs|pbcopy; kill -9 $(pbpaste);echo my_password|pbcopy; sudo
umount path/to/my/mount/folder;
it works perfectly. If I however add it to my .bash_profile as an alias:
alias mount-reset="pgrep sshfs|pbcopy; kill -9 $(pbpaste);echo my_password|pbcopy; sudo umount path/to/my/mount/folder;"
It jumps over the pbpaste command and echoes my_password. I always thought that the alias commands are executed exactly as if I would write them to the terminal, but why is this not working? How do I pbpaste in alias script? What fundamentals have I understood wrong?
You need to wrap the alias in single-quotes, rather than double-quotes. The relevant difference is that bash expands $something (including $(command)) inside double-quotes before executing the command. The way you have it at the moment, pbpaste is executed when .bash_profile runs, and whatever happens to be in the paste buffer at that time gets included in the alias. With single-quotes, it includes $(pbpaste) directly in the alias, so it gets expanded when you use the alias.
alias mount-reset='pgrep sshfs|pbcopy; kill -9 $(pbpaste);echo my_password|pbcopy; sudo umount path/to/my/mount/folder;'
However, there's an easier way. I don't see any reason to pass the sshfs PID through the paste buffer; just use include it directly:
alias mount-reset='kill -9 $(pgrep sshfs);echo my_password|pbcopy; sudo umount path/to/my/mount/folder;'
...but that's still more complicated than it needs to be, because OS X includes a killall command which kills processes by name, thus eliminating the need to pgrep:
alias mount-reset='killall -9 sshfs;echo my_password|pbcopy; sudo umount path/to/my/mount/folder;'
(BTW, some unixes include a command named killall that does something much more dangerous -- this alias is not portable!)
In OS X alias commands should be added to ~/.bashrc
When bash is an interactive non-login shell it uses .bashrc, not .bash_profile. If bash is invoked as an interactive login shell, it uses .bash_profile, not .bashrc.
Try adding the alias to ~/.bashrc. If you wanted to add a mount-reset command as an interactive login shell command into ~/.bash_profile you could use something like this:
# kill sshfs
mount-reset () { pgrep sshfs|pbcopy; kill -9 $(pbpaste); echo my_password|pbcopy;
sudo umount path/to/my/mount/folder; }

How to launch tmux with pre-opened second window and execute commands?

There's possible duplicate but it's closed and unanswered.
As I'm using chef for automation, would be nice to automate tmux launch with pre-launched python web server and second window opened. (this is specifically for my development environment). And only way of doing this probably specifying parameters from command line.
Commands which I want to execute are in window with title "daemon":
source bin/activate
cd project
DEBUG=1 python app.py
I'm unable to find which command line parameters allows to pre-execute commands when launching tmux, and also to open more windows on launch.
You want to create a session without attaching to it (using the -d option), so that you can send additional tmux commands to open the second window before actually attaching.
tmux new-session -t mysession -d 'source bin/activate; cd project; DEBUG=1 python app.py'
tmux new-window -t mysession
tmux attach-session -t mysession

How to stop making tmux auto setting RBENV_VERSION

tmux is auto setting RBENV_VERSION when I start tmux...
Anyone know how to stop it?
Because it auto sets it, I need to do
$ export RBENV_VERSION
to unset it and make .ruby-version work. Thx.
tmux itself will never set (or unset) RBENV_VERSION of its own accord. You have some bit of configuration that is causing this.
My guess is that RBENV_VERSION was set when you started your tmux server and that is is now part of the tmux “global environment” (the base environment inherited by all the processes started by tmux). You can check this
tmux show-environment -g | grep RBENV
If it is present there, you can delete it with this command:
tmux set-environment -gu RBENV_VERSION
If you often find yourself starting tmux when RBENV_VERSION is already set (and you do not want it sent “inside” tmux), then you can add the above command to your ~/.tmux.conf file to make sure that it is cleared every time you start a server.
Another possibility is that it is part of your tmux “session environment”; this environment is “layered” atop the global environment to form the environment that is inherited by the processes started for new windows and panes in a session. You can check it with this command (run it inside the session, or add -t sessname to specify a session):
tmux show-environment | grep RBENV
If this is present, you can unset it in a similar way:
tmux set-environment -u RBENV_VERSION
Finally, if the variable is not present in either the global or session environments, then it is probably coming from something in your shell initialization files. By default, tmux starts login shells, so be sure to check the corresponding bits of shell configuration (e.g. .bash_profile, .bash_login, .profile, etc.) as well as any other bits of initialization.

Resources