Terminal opened in vim doesn't have .bash_profile sourced implicitly - bash

I am running vim 8.2 and when I execute the command "term" inside vim, it opens up a terminal in the same window but it doesn't have the .bash_profile sourced implicitly. I need to do that before using the terminal.
Is there a way to fix this?

According to the bash man page:
.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists.
Since you are using, non-login shell, I would suggest you to put all your aliases, shell options in the bashrc.

Related

Issue with aliases in Terminal shell

I'm set /bin/bash as my default shell in Terminal (macos) and my aliases, written in '~/.bashrc' don't work (command not found).
But if I write in Terminal /bin/bash command, I'll switch to new bash3.2 shell, and then I can use it.
Is there some another '.bashrc' where I should write down my aliases?
P.S. I used Terminal preferences to made /bin/bash default
and chsh -s /bin/bash/ command too.
Terminal starts a login shell rather than an "ordinary" interactive shell, because Terminal itself is not run from a shell that inherited an environment from a login shell.
Ordinary interactive shells source .bashrc; login shells source the first of .profile, .bash_login, or .bash_profile it finds. I recommend sourcing .bashrc directly from .bash_profile.
Your aliases work when you run /bin/bash directly because that does not start another login shell, and so .bashrc is sourced as expected.
As above, if you add the following line to .profile in your home directory:
source ~/.bashrc
Once you save it and create a new Terminal session, it will automatically source your .bashrc with your alias settings.
You want /bin/bash -l to run as if you used a login shell. See the INVOCATION section of man bash for more information.

Difference when running 'Cygwin.bat' and if just running 'bash' (.bash_profile vs. .bashrc)

When I am running Cygwin.bat I've got my all my custom stuff working from .bash_profile but when I am just running bash none of my stuff from .bash_profile is working and I am just got wired prefix like root#comp:/mnt/c/cygwin64# (as my current dir)
Is there any way to achieve the same result when running bash as I got when running Cygwin.bat
the content of Cygwin.bat is:
#echo off
C:
chdir C:\Tools\cygwin64\bin
bash --login -i
As pointed out by #matzeri in the comment, cygwin.bat invokes bash with the --login option which creates an interactive login shell. And bash without the --login option creates an interactive shell which is not a login shell.
According to bash man page:
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.
When an interactive shell that is not a login shell is started, bash reads
and executes commands from ~/.bashrc, if that file exists. This may be
inhibited by using the --norc option. The --rcfile file option will force
bash to read and execute commands from file instead of ~/.bashrc.
My ~/.bash_profile has only one line:
source ~/.bashrc
and I put all conf in ~/.bashrc.

"source" command on mac

I have a .bashrc file on Mac OS in which I set some aliases. After I make it and I type source .bashrc, the aliases work perfectly. However if open another shell, my shortcut command will not be recognized and I need to do source .bashrc again. How can it make it once and for all?
Terminal and iTerm 2 open new shells as login shells by default. When Bash is opened as a login shell, it reads ~/.bash_profile but not ~/.bashrc.
See https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html:
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.
So either:
Use ~/.bash_profile instead of ~/.bashrc.
Use ~/.bashrc but source ~/.bashrc from ~/.bash_profile.
Tell your terminal application to open new shells as non-login shells.
I have done the last two. For example tmux and the shell mode in Emacs open new shells as non-login shells. I still source ~/.bashrc from ~/.bash_profile because Bash is opened as a login shell when I ssh to my computer.
If you are on Mac and you want to source ~/.bash_profile automatically on when a terminal is opened
Open Terminal
Go to Preferences
Go to Profiles
Select then open shell
Add this command source ~/.bash_profile in Run command
If you want to source your bash_profile file everytime you open a new tab you can also set up a command on Iterm.
Go to Preferences -> Profiles -> General -> Command
I encounter the same problem and I solve it.
The macos have shift the default shell from bash to zsh.
So I try to modify the ~/.bashrc and ~/.bash_profile and source that but just work to current Terminal.
The fact is you should modify the ~/.zshrc profile file.
Try it my friend!
nano ~.bash_profile (Opens the ~.bash_profile)
aliasname(){
ssh user#ipaddress
}(You can add any command for the alias, I have shown the ssh command for a particular IP address)
Press Control+O and press Enter (Save the file). Control+X(Exit nano editor)
source .bash_profile
Now you have a persistent command across all the terminals whenever you type aliasname.

What is the difference between the various shell profiles?

What's the difference between ~/.bashrc, ~/.bash_login, ~/.bash_logout, ~/.bash_profile, ~/.profile, /etc/profile, /etc/bash.bashrc, /etc/ssh/ssh_config and sshd_config, when are they loaded and what are their purposes?
The man page for bash says there are the following initialization files for bash shells:
/etc/profile
The systemwide initialization file, executed for login shells
/etc/bash.bashrc
The systemwide per-interactive-shell startup file
/etc/bash.bash.logout
The systemwide login shell cleanup file, executed when a login shell exits
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
~/.bash_logout
The individual login shell cleanup file, executed when a login shell exits
~/.inputrc
Individual readline initialization file
Apparently there seem to be different configuration files for the different shells (bash, zsh, csh, and others). There seem to be as many shells as different linux and unix versions: csh, ksh, bash, zsh, ... Bash has a .bashrc, Zsh has a .zshrc, etc. One can also distinguish between login shells and non-login shells and between system-wide defaults and user-specific defaults.
It makes sense to distinguish between login and non-login shells, because some commands should be processed only at login, while other commands should run everytime you open a new terminal window. That is the difference between .bash_profile and .bashrc. For bash the .bashrc is reloaded every time you start a new copy of bash, i.e. when you start a new bash but do not login. The .bash_profile or .profile is loaded only when you login. The abbtreviation rc in bashrc stands for "run commands" or "run control" and is a convention adopted from older Unix systems.
system-wide defaults for..
/etc/profile ..login shells, for interactive shells with login
/etc/bashrc ..non-login Bash shells
user-specific defaults in home directory ~ for..
~/.profile ..login shells, called after login
~/.bashrc ..non-login shells, if already logged in
~/.bash_profile ..login shells, called after login (lower priority)
user-specific defaults in home directory for login and logout
~/.bash_login ..login shells (called upon login)
~/.bash_logout ..login shells (called upon logout)
The following links were helpful: .bashrc vs .bashprofile and .bash_profile vs .bashrc, the bash manual page (man bash), and Zsh/Bash startup files loading order (.bashrc, .zshrc etc.).
I happen to be curious about these files and did some experiment myself. It turns out to be a little different than what is in the documents.
I know the differences between interactive and non-interactive or login and non-login.
I tried on two computers, my macbook pro with OS 10.9 and a server with ubuntu server 13.10. I add the following command into the /etc/profile:
echo "Loading /etc/profile"
And similar commands into /etc/bash.bashrc, /etc/bashrc, /etc/bash.bashrc, ~/.profile, ~/.bash_profile, ~/.bashrc, ~/.bash_login and make sure that these files do not source each other inside themselves.
(OS 10.9, GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13))
On the mac, with interactive login bash, I have:
Loading /etc/profile
Loading ~/.bash_profile
Which means that the files loaded directly are only /etc/profile and ~/.bash_profile.
with interactive non-login bash, I have:
Loading ~/.bashrc
which means that the file loaded directly is ~/.bashrc.
(ubuntu server 13.10 GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu))
On the ubuntu, with interactive login bash, I have:
Loading /etc/profile
Loading ~/.bash_profile
Which means that the files loaded directly are only /etc/profile and ~/.bash_profile.
with interactive non-login bash, I have:
Loading /etc/bash.bashrc
Loading ~/.bashrc
which means that the files loaded directly are /etc/bash.bashrc and ~/.bashrc.
I do not know why~

Should aliases go in .bashrc or .bash_profile? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the difference between .bashrc, .bash_profile, and .environment?
It seems that if I use
alias ls='ls -F'
inside of .bashrc on Mac OS X, then the newly created shell will not have that alias. I need to type bash again and that alias will be in effect.
And if I log into Linux on the hosting company, the .bashrc file has a comment line that says:
For non-login shell
and the .bash_profile file has a comment that says
for login shell
So where should aliases be written in? How come we separate the login shell and non-login shell?
Some webpage say use .bash_aliases, but it doesn't work on Mac OS X, it seems.
The reason you separate the login and non-login shell is because the .bashrc file is reloaded every time you start a new copy of Bash. The .profile file is loaded only when you either log in or use the appropriate flag to tell Bash to act as a login shell.
Personally,
I put my PATH setup into a .profile file (because I sometimes use other shells);
I put my Bash aliases and functions into my .bashrc file;
I put this
#!/bin/bash
#
# CRM .bash_profile Time-stamp: "2008-12-07 19:42"
#
# echo "Loading ${HOME}/.bash_profile"
source ~/.profile # get my PATH setup
source ~/.bashrc # get my Bash aliases
in my .bash_profile file.
Oh, and the reason you need to type bash again to get the new alias is that Bash loads your .bashrc file when it starts but it doesn't reload it unless you tell it to. You can reload the .bashrc file (and not need a second shell) by typing
source ~/.bashrc
which loads the .bashrc file as if you had typed the commands directly to Bash.
Check out http://mywiki.wooledge.org/DotFiles for an excellent resource on the topic aside from man bash.
Summary:
You only log in once, and that's when ~/.bash_profile or ~/.profile is read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variables in there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
Once you log in, you can run several more shells. Imagine logging in, running X, and in X starting a few terminals with bash shells. That means your login shell started X, which inherited your login shell's environment variables, which started your terminals, which started your non-login bash shells. Your environment variables were passed along in the whole chain, so your non-login shells don't need to load them anymore. Non-login shells only execute ~/.bashrc, not /.profile or ~/.bash_profile, for this exact reason, so in there define everything that only applies to bash. That's functions, aliases, bash-only variables like HISTSIZE (this is not an environment variable, don't export it!), shell options with set and shopt, etc. For an example, see: My .bashrc
Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrc but only ~/.profile or ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profile too: source ~/.bashrc.
From the bash manpage:
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.
When a login shell exits, bash
reads and executes commands from the
file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.
Thus, if you want to get the same behavior for both login shells and interactive non-login shells, you should put all of your commands in either .bashrc or .bash_profile, and then have the other file source the first one.
.bash_profile is loaded for a "login shell". I am not sure what that would be on OS X, but on Linux that is either X11 or a virtual terminal.
.bashrc is loaded every time you run Bash. That is where you should put stuff you want loaded whenever you open a new Terminal.app window.
I personally put everything in .bashrc so that I don't have to restart the application for changes to take effect.

Resources