Can one configure Tmux to load ~/.bashrc instead of ~/.bash_profile? - linux

Right now, when I log in to Tmux, only ~/.bash_profile gets loaded.
I'd like ~/.bashrc to get called instead.
Is that possible?

You can fix that at the tmux level by explicitly setting the default shell command:
tmux set-option default-command "/bin/bash"
From the tmux manual (emphasis mine):
default-command shell-command
Set the command used for new windows (if not specified
when the window is created) to shell-command, which may
be any sh(1) command. The default is an empty string,
which instructs tmux to create a **login shell** using the
value of the default-shell option.
default-shell path
Specify the default shell. This is used as the login
shell for new windows when the default-command option is
set to empty, and must be the full path of the exe‐
cutable. When started tmux tries to set a default value
from the first suitable of the SHELL environment vari‐
able, the shell returned by getpwuid(3), or /bin/sh.
This option should be configured when tmux is used as a
login shell.
As explained by Chepner in a comment below:
default-shell defaults to your preferred login shell;
default-command defaults to starting a login instance, effectively
$SHELL -l
... and in the case of Bash, a login shell doesn't read ~/.bashrc. By overriding the value of default-command, we can now force tmux to create a non-login shell instead.

This issue is not related to tmux. To solve it make sure to add source ~/.bashrc to .bash_profile and that's it.
You can learn more about bash initialization and which files it loads and in which order here: https://github.com/sstephenson/rbenv/wiki/Unix-shell-initialization#bash
As you can see .bashrc is not even on the list when bash is started in login mode, that's why we source it from the file (.bash_profile) that is on the list.

Related

How to Change my default shell on server?

I was assigned an account for log in to a remote server, and I want to change my default shell.
I tried chsh command but it says: chsh: "/public/home/{my_id}/bin/zsh" is not listed in /etc/shells.
If you don't have permission to install zsh system wide, a quick fix is to append exec ~/bin/zsh -l to ~/.bash_profile (if bash is the current shell), or an equivalent rc file for the current login shell.
zsh -l starts zsh as a login shell.
exec COMMAND replaces the current process with COMMAND, so you'll only have to type exit (or press ctrl+d) once.
~/.bash_profile is executed when bash starts as a login shell, you can still run command bash normally.
Depending what is in ~/.bash_profile (or equivalent), you may wish to avoid executing its other contents, by putting exec ~/bin/zsh -l at the start of the file (not the end), and copy/port anything important over to the zsh equivalent, $ZDOTDIR/.zprofile.
I might also do export SHELL="$HOME/bin/zsh", although I'm unsure of the full effects of setting SHELL differently to that specified for your user in /etc/passwd, to a shell not in /etc/shells, and to a shell binary in your home path.
First check all the shells available on your linux system
cat /etc/shells
Use chsh command line utility for changing a login shell with the -s or –shell option like this.
# chsh --shell /bin/sh tecmint

Why is alias not available in /etc/profile?

I put alias se='sudo -E ' in /etc/profile and rebooted. Then I typed se and terminal just said "command not found".
Why is alias not available in /etc/profile? How to solve it?
EDIT:
I have tried put alias se='sudo -E ' in /etc/profile, ~/.zprofile, ~/.zshrc
/etc/profile failed
/etc/zprofile failed
/etc/zshenv successful
/etc/zshrc successful
~/.zprofile failed
~/.zshrc successful
REF:
https://unix.stackexchange.com/questions/38175/difference-between-login-shell-and-non-login-shell
https://en.wikipedia.org/wiki/Shell_(computing)
https://en.wikipedia.org/wiki/X_Window_System
As you are using /etc/profile, i am assuming that you are looking to do this only for login sessions of shell (zsh).
That's because zsh by default does not read /etc/profile when starting a login shell. For setting any global parameter for only login shells, use the file /etc/zsh/zprofile.
For setting something applicable on any sort of invocation, use /etc/zsh/zshenv instead. The typical/generic order is zshenv > zprofile > zshrc > zlogin (depending on invocation and availability).
Needless to say, for any user specific parameter you should use the user specific .zprofile i.e. ~/.zprofile instead.
Notes:
If you are looking to do this for any interactive shell session, use .zshrc (/etc/zshrc or ~/.zshrc)
If you are looking at using a common /etc/profile (or similar), source (.) /etc/profile from the relevant file for zsh.

Closing the terminal doesnt remember my .profile changes for nvm/npm

I installed nvm/npm using this instruction
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-an-ubuntu-14-04-server (How To Install Using NVM), but everytime I close my terminal, it seems to forget all the settings and I have to do the command:
source ~/.profile then select the npm version to make it run again. How can I keep my settings permanent, or at least for the duration of my logged in session? Thanks! (linuxmint 17)
Sourcing ~/.profile
~/.profile is typically invoked from a login shell. See http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files
Invoked as an interactive login shell, or with --login
When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Opening a terminal does not typically invoke a login shell. Have you tried rebooting/relogging? Additionally if either ~/.bash_profile or ~/.bash_login exist, then bash won't source ~/.profile. There are two common ways to fix this:
Option 1
Move commands from the ~/.profile file to ~/.bash_profile
Option 2
Source ~/.profile from ~/.bash_profile:
[[ -f ~/.profile ]] && . ~/.profile
Default Node version
You can configure a default node version by aliasing it to default
To set a default Node version to be used in any new shell, use the alias 'default':
nvm alias default node
See https://github.com/creationix/nvm

preserve my env in linux screen

I have serveral customized environment variables and functions in my ~/.bash_profile which I would like to be called automatically when I start my screen session with screen -D -R, but apparently adding source ~/.bash_profile in ~/.screenrc doesn't solve my issue.
On every new screen I have to manually source the bash_profile.
I have noticed that this file can't be found while creating a new screen (when you create new screen there is the the line in the bottom showing what commands are executing and it returns that the file can't be found.)
~/.bash_profile is only run by login shells.
~/.bashrc is run by other interactive shells, such as those started by screen.
~/.screenrc is a screen configuration file, and is not used to configure bash.
Put things that should be set up per shell in ~/.bashrc, and add source ~/.bashrc to your ~/.bash_profile.
You can try configuring screen to run a login-shell by default.
Screen runs a sub-shell, unless told otherwise (See "shell" .screenrc command). Example .screenrc entry to run a login-shell:
shell -$SHELL

How can I debug the bash prompt?

I've been editing .bashrc files and other init files, and it seems that I've left behind a few code snippets or two that are causing a few errors at the prompt (e.g. file missing), but I can't find them.
How do I debug the prompt to find out what init scripts I've carelessly hacked?
Most of the shells have debug flags that show the commands being executed. Bash may even have one that shows a command before expansion of variables and after. Have you tried checking (I believe) -c -x or -X flags and see if they show the information you are looking for.
You can set them as first thing in the rc files (most global one) or just pass it down into bash command by invoking it from another shell.
In fact, if you invoke bash from another shell, you can also use script command to record everything you see and do into the file, which makes postmortem analysis so much easier.
Try invoking bash with the -x flag, then sourcing your .bashrc or .bash_profile or whatever you're using. That ought to be prolix enough to find your problem
ie:
bash -x
source .bashrc
The easiest way to get a clean initial state is to SSH into your current host, but instead of letting SSH launch your shell with default settings, you provide an explicit command which prevents .bashrc from being read.
ssh -tt localhost /bin/bash --norc
The -tt forces SSH to allocate a TTY, which is what would normally happen when you open a shell connection, but is not default when running an explicit command.
The --norc prevents bash from reading your settings file (since we want to do that ourselves).
You should now be at a bash prompt, in a clean environment. This is useful for examining what variable are set to before your .bashrc runs etc. Enable tracing and source your .bashrc:
set -x # Enable tracing
source .bashrc
Try to see where you've defined prompt - probably it in some dot file in your home directory:
grep PS1 ~/.*
You can see current value of prompt by just printing it:
echo $PS1
HTH
Check the .bash_history file in your home directory to find out what commands you have been running. If you used commands like vi filename to open the init scripts, it will find them in the command history.

Resources