How to load ~/.bash_profile when entering bash from within zsh? - bash

I've used bash for two years, and just tried to switch to zsh shell on my OS X via homebrew. And I set my default (login) shell to zsh, and I confirmed it's set properly by seeing that when I launch my Terminal, it's zsh shell that is used in default.
However, when I try to enter bash shell from within zsh, it looks like not loading ~/.bash_profile, since I cannot run my command using aliases, which is defined in my ~/.bash_profile like alias julia="~/juila/julia", etc.. Also, the prompt is not what I set in the file and instead return bash-3.2$.
For some reasons, when I set my login shell to bash, and enter zsh from within bash, then ~/.zshrc is loaded properly.
So why is it not loaded whenever I run bash from within zsh? My ~/.bash_profile is symbolic linked to ~/Dropbox/.bash_profile in order to sync it with my other computers. Maybe does it cause the issue?

Open ~/.zshrc, and at the very bottom of the file, add the following:
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile;
fi
Every time you open the terminal, it will load whatever is defined in ~/.bash_profile (if the file exist). With that, you can keep your custom settings for zsh (colors, and etc). And you get to keep your custom shell settings in .bash_profile file.
This is much cleaner than using bash -l IMO.
If you prefer putting your settings in .bashrc, or .bash_login, or .profile , you can do the same for them.
Similarly, you could also move the common profile settings to separate file, i.e. .my_common_profile, and add the following to both .bash_profile and .zshrc:
if [ -f ~/.my_common_profile ]; then
. ~/.my_common_profile;
fi

An interactive bash reads your ~/.bash_profile if it's a login shell, or your ~/.bashrc if it's not a login shell.
A typical .bash_profile will contain something like:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
so .bashrc can contain commands to be executed by either login or non-login shells.
If you run bash -l rather than just bash, it should read your .bash_profile.
Reference: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

For those who have just installed zsh and want their alias from bash to work on zsh do the following
Open .zshrc file in vim like so
vi ~/.zshrc
Scroll to the bottom
click "i" to enable write mode
Tell zsh to load items from bash_profile when needed like so
source ~/.bash_profile
Write and quit like so
:wq
Refresh your zsh like so
source ~/.zshrc
That's it. Now all your saved alias in .bash_profile will be ready to use in zsh.

To complement #Keith Thompson's excellent answer:
macOS:
As #chepner puts it succinctly (emphasis mine):
In OS X, bash is not used as part of the initial [at boot time] login process, and the Terminal.app (or other terminal emulators) process exists outside any pre-existing bash sessions, so each new window [or tab - read: interactive bash shell] (by default) treats itself as a new login session.
As a result, some OSX users only ever create ~/.bash_profile, and never bother with ~/.bashrc, because ALL interactive bash shells are login shells.
Linux:
On Linux, the situation is typically reversed:
bash shells created interactively are [interactive] NON-login shells, so it is ~/.bashrc that matters.
As a result, many Linux users only ever deal with ~/.bashrc.
To maintain bash profiles that work on BOTH platforms, use the technique #Keith Thompson mentions:
Put your definitions (aliases, functions, ...) in ~/.bashrc
Add the following line to ~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc

Copy the contents from ~/.bash_profile and paste them at the bottom of ~/.zshrc file.

For ZSH users on MacOs, I ended up with a one liner.
At the very bottom of the ~/.zshrc I added the following line :
bash -l
What it does is simply load the .bash_profile settings(aliases, function, export $PATH, ...)
If you decide to get rid of ZSH and go back to plain BASH, you'll be back to normal with no hassle at all.

If this is something that you do infrequently, or it just isn't appropriate to make changes, you can also 'source' the .bash_profile after launching the child bash shell.
. ~/.bash_profile
This will pull in the settings you make in the .bash_profile script for the life of that shell session. In most cases, you should be able to repeat that command, so it's also an easy way to test any changes that you make without needing to do a full login, as well as bring all of your existing shell sessions up-to-date if you make upgrades to the .bash_profile &/or .bashrc files.

For macOS Big Sur (Version 11.5.2)
Open .zshrc
For example: sudo nano ~/.zshrc
At the end of the file add source ~/.bash_profile
Every time you open the terminal the contents inside the bash profile will be loaded.

Recently I installed oh-my-zsh on OS X and set zsh as default shell and faced the same problem.
I solved this problem by adding source ~/.bash_profile at the end of .zshrc file.

I am using a zsh framework called oh my zsh and I have tried most of the solutions listed here and it broke the format for my custom theme. However, these steps worked for me.
Add new alias(es) at the bottom of my .bash_profile
vi ~/.bash_profile
Make zsh to load items from .bash_profile
source ~/.bash_profile
Refresh zsh
source ~/.zshrc
Restart OSX Terminal app
Try your new alias!

If you'd like to be "profile-centric", you can create .profile as a single source of truth, then load it from both .bash_profile and .zprofile.
.profile
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# etc., etc.
.bash_profile and .zprofile
if [ -f ~/.profile ]; then
. ~/.profile;
fi
I found this helped bash scripts find the right PATH, etc., and helped me keep configuration in one place.

Related

~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up

I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.
Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL to confirm if this is the case for you.
Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following:
echo "source ~/.bash_profile" >> ~/.zshrc
Open a new terminal window for the changes to take effect.
Two things need to happen here when using iTerm to get the loading of dotfiles to work.
First you should add the following to your .bash_profile
[[ -s ~/.bashrc ]] && source ~/.bashrc
Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.
Hope this helps!
Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.
Preferences > Profile > Startup > Add command 'source ~/.bash_profile'
Mac terminal preferences window screenshot
Might be considered to be a bit hacky, but it does the trick.
Adding source ~/.profile to my .bash_profile worked for me.
As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:
source ~/.bash_profile
By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.
As of Catalina the default shell is now zsh. You can change it back to bash with chsh -s /bin/bash and that should load your .profile or .bash_profile
Why are your shell's initialization files not loading?
As with most things, It Depends ™
I recently experienced the same phenomenon and went through the following exercise to resolve it:
I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell
Therefore, I know that ~/.bash_profile will always be called.
Knowing that, I put the following in my ~/.bash_profile file:
for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
[ -r "$file" ] && source "$file"
done
unset file
Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.
Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash
So, I started with my ~/.bash_profile file.
I found that when I installed Canopy Express that it's installer replaced the contents of my ~/.bash_profile file with the following content:
# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'
p.s. Canopy is an excellent, free python IDE, that I highly recommend.
Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.
My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.
Oh, and you may want to verify which shell you are using (I use bash):
~ $ echo $SHELL
/usr/local/bin/bash
I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.
Put source .bash_profile into your appropriate 'bashrc' file will make the auto loading recovered, i.e.
.login for tcsh, .bash_profile for bash, .zshrc for zsh
My issue was solved by unchecking Preferences > General > tmux >
Use "tmux" profile rather than profile of the connecting session
Most likely, you need to create the files yourself as they appear not to exist by default. You should give them execute permission to make them run.
~ % sudo chmod 700 ~/.bash_profile
Also, you should check the ownership of the files. They should belong to current user rather than root. Otherwise, you will get permission denied error.
~ % ls -a -l
~ % sudo chown <user_name> ~/.bash_profile
Finally, please note that bash looks in your home directory for .bash_profile, .bash_login, and .profile in order. Bash will stop looking if the first is found.
This means if you have both .bash_profile and .profile files, the .profile will not run.
For more information
Hope this would help you.
Little late to the party but it seems that the file .zprofile is the equivalent to that of .bash_profile when loading zsh. I used this instead to execute a few commands on startup. Of course this only valid for a specific iTerm setup with zsh.
https://zsh.sourceforge.io/Intro/intro_3.html

Is there anything in Zsh like .bash_profile?

Everytime I open the terminal, I have to source .bash_profile to enable the $JAVA_HOME or other variables.
Yes, it's called ~/.zshenv.
Here's how I have $JAVA_HOME set in ~/.zshenv:
export JAVA_HOME="$(/usr/libexec/java_home)"
Keep in mind, however, that zsh is not bash, so just 'cause you have to source your .bash_profile every time you open a terminal does not mean that you have to do that with zsh. With zsh, I only have to re-source my ~/.zshenv when I make changes to it, and then only for terminals which are already open: new terminals should have already sourced my new and improved ~/.zshenv.
NOTE
I often find it helpful, when trying to determine which of my zsh startup files I should place things in to consult zsh startup files.
A newer version of the documentation for startup files can be found here.
I know this is an old question, but I recently upgraded MacOs to Catalina which changed the default shell from bash to zsh.
I ended up doing this:
echo source ~/.bash_profile > ~/.zshenv && source ~/.zshenv
To have zsh source my original .bash_profile.
Recently, with the upgrade to macOS Catalina, the default shell changed to zsh, which uses ~/.zshrc as the resource file.
We usually had ~/.bash_profile inside user home directory the solution is to simply
Open ~/.bash_profile by running vim ~/.bash_profile
Open ~/.zshrc by running vim ~/.zshrc
Copy the content of ~/.bash_profile into ~/.zshrc
Open a new terminal window and run your previous aliases/scripts, which should work flawlessly.
Other simple alternative to continue using your .bash_profile is add this file to your .zshrc file:
Open your .zhsrc file > vim ~/.zshrc
Add this line to your .zshrc file > source ~/.bash_profile
with this simple solution you can continue adding your .bash_prifile if you like zhs.
Adding .bash_profile
There are five separate profile scripts that get executed (in the order given below) when we launch a zsh shell or close it out.
(1) .zshenv --> This is always sourced first but can be overridden by other
(2).zprofile --> This is equivalent for users coming from ksh experience
(3).zshrc --> This is for all of the interactive customizations of zsh
(4).zlogin --> This executes after first three are done
(5).zlogout --> This is executed when we logout of the zsh shell
it would be advisable to put your stuff in .zshenv or in .zshrc
It is not mandatory to have any one of these files. But if it is there, it will be sourced from and executed in the above order.
In Mac Catalina onwards osx versions, the terminal uses zsh. There is a system-wide profile /etc/zprofile.
cat /etc/zprofile
# System-wide profile for interactive zsh(1) login shells.
# Setup user specific overrides for this in ~/.zprofile. See zshbuiltins(1)
# and zshoptions(1) for more details.
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
it says , if you want to override then create ~/.zprofile.
touch ~/.zprofile.
update: macOS Monterey 12.4
yes - for Zsh, it is the file: .zshrc
add there your parameter.
In Mac Catalina, terminal uses zsh. Instead of having .bash_profile, good to have .zshenv and write your script there.
When you open terminal next every time, scripts inside .zshenv gets executed.
I was running into this issue and I followed Zack and Luke Schoen's answer, but my $PATH didn't look the same as what I had in bash.
This post explains what the different config files do:
https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout
I found that splitting my .bash_profile path exports into .zprofile and my aliases into .zshrc worked best for what I wanted.
I found why Zack and Luke Schoen's answer didn't work for me:
The path exports that I listed in .zshenv were executed first and /usr/libexec/path_helper was executed afterwards,
which prepended the paths listed in /etc/paths.
I found the profile file under /etc/zprofile location. This will be for zsh

How to make OS X to read .bash_profile not .profile file

I have read so many suggestions about, not putting your customization aka commands in ".profile" file. Rather, create a .bash_profile for yourself and add your alias and etc.
But,when I open the new terminal, if there is only .bash_profile, OS X is not exporting/sourcing the commands mentioned in it. I have to manually source the .bash_profile.
If I create .profile file, on opening a new terminal, all my commands in .profile are executed and will be available readily.
Could you please help me in understanding, how does it works? Also, when to use .bashrc/.profile/.bash_profile files.
Thanks!
According to Apple,
zsh (Z shell) is the default shell for all newly created user accounts, starting with macOS Catalina.
So you should verify your default shell with the command:
$ echo $SHELL
If the result is /bin/bash your default shell is BASH, and if the result is /bin/zsh the default is ZSH.
Go to home with $ cd ~/ and create the profile (if it does not exist) and edit it with the commands:
For bash:
$ touch .bash_profile
$ open .bash_profile
For ZSH:
$ touch .zprofile
$ open .zprofile
According to the manual page that ships with OS X:
... 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.
It should only read ~/.profile as a last resort if neither ~/.bash_profile nor ~/.bash_login are readable.
On all of my OS X systems, I have my ~/.bash_profile set to:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
It is highly recommended that you do this on OS X in order to get bash to read your ~/.bashrc file like you would expect.
It's also possible that your terminal shell is defaulting to sh instead of bash. You can verify this first:
$ echo $SHELL
/bin/tcsh
To change this to bash, you can go into your Terminal -> Preferences -> Startup tab, and change "Shell Opens With:" from "Default login shell" to Command and value "/bin/bash".
Alternately, you can change your default shell by executing the following command at the command prompt:
chsh -s bin/bash
After you do one of these, open a new shell window, and your .bash_profile should be sourced.
For anyone else who finds this, instead of bash_profile, for new versions of mac you can use .zshrc. I.E., do
open .zshrc
and add what you need there.
You can use zsh to fix the problem.
The Z shell (also known as zsh) is a Unix shell that is built on top
of bash (the default shell for macOS) with additional features. It's
recommended to use zsh over bash.
Installation
Install zsh using Homebrew: $ brew install zsh
Install Oh My Zsh: $ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Move to .bash_profile setting .zshrc file
To apply the changes you make you need to either start new shell
instance or run: source ~/.zshrc
If you are using zsh, you can source to .bash_profile by adding the following line to .zprofile
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
It should be mentioned that bash will first look for a /etc/profile file, as stated in the Bash man pages.
When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands 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.
I solved by simply adding bash (in a newline) into ~/.bash_profile file.

MacVim doesn't load $PATH env variable from .bash_profile [duplicate]

I am using MacVim (basically gvim for the mac).
If I open macvim from the command line then my $PATH variable will be properly set.
If I open macvim via point and click with the finder, the $PATH variable will NOT be properly set.
Can anyone give me some insight?
Note: I know at least part of my path is set in ~/.bashrc, but I am not sure where the rest of it is set.
Examples:
If I open macvim from the terminal:
% gvim basic.tex
And then in MacVim I go:
:!echo $PATH
/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/Applications/MacVim.app/Contents/M
acOS:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/texbin:/usr/X11R6/bin
This is the right path.
When I open the file with the mouse (in finder)
When I go:
:!echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin
It gives me a little path. Why?
I had this same issue but it only appeared after setting my default shell to zsh like so
export SHELL=/bin/zsh
It seems that there is a bug in the OS X zsh setup. The work around in brief is to merge /etc/zshenv into /etc/zprofile. In my case I didn't have a /etc/zprofile so just moving over the file did the trick:
sudo mv /etc/zshenv /etc/zprofile
This post describes the solution in more detail.
For me, simply creating a new symbolic link from .zprofile to .zshrc did the trick:
ln -s ~/.zshrc ~/.zprofile
The place to set environment variables
on the Mac for GUI applications (those started via loginwindow, the
Finder, etc.) is ~/.MacOSX/environment.plist
Alternately in MacVim you can choose to launch vim processes in a login-shell (look in the preferences).
For more info see this post.
The difference in the PATHs probably has something to do with the difference between a login shell (logging in) and a non-login shell (bringing up a console).
From the bash man page:
When bash is invoked as an interactive login shell ... it looks for ~/.bash_profile...
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc...
What I did to get around this issue was to add the following code to my ~/.bash_profile, telling it to source my ~/.bashrc if it exists:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

Terminal: Where is the shell start-up file?

I'm following a tutorial called Starting a Django 1.4 Project the Right Way, which gives directions on how to use virtualenv and virtualenvwrapper, among other things.
There's a section that reads:
If you're using pip to install packages (and I can't see why you wouldn't), you can get both virtualenv and virtualenvwrapper by simply installing the latter.
$ pip install virtualenvwrapper
After it's installed, add the following lines to your shell's start-up file (.zshrc, .bashrc, .profile, etc).
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/directory-you-do-development-in
source /usr/local/bin/virtualenvwrapper.sh
Reload your start up file (e.g. source .zshrc) and you're ready to go.
I am running Mac OSX, and don't know my way around the Terminal too well. What exactly does the author mean by shell's start-up file (.zshrc, .bashrc, .profile, etc)? Where do I find this file, so that I can add those three lines?
Also, what does he mean by reload your start up file (e.g. source .zshrc)?
I would appreciate a detailed response, specific to OSX.
You're probably using bash so just add these 3 lines to ~/.bash_profile:
$ cat >> ~/.bash_profile
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/directory-you-do-development-in
source /usr/local/bin/virtualenvwrapper.sh
^D
where ^D means you type Control+D (EOF).
Then either close your terminal window and open a new one, or you can "reload" your .bash_profile like this:
$ source ~/.bash_profile
If you use bash, it usually means ~/.bash_profile.
In Terminal and iTerm new shells are login shells by default, so ~/.bashrc is not read at all. If instructions written for some other platform tell you to add something to .bashrc, you often have to add it to .bash_profile instead.
If both ~/.profile and ~/.bash_profile exist, only .bash_profile is read. .profile is also read by other shells, but many of the things you'd add to .bash_profile wouldn't work with them.
From /usr/share/doc/bash/bash.html:
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.
[...]
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.
I have Anaconda install, so I add these 3 lines to ~/.bash_profile
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Documents/Python
source /Users/Username/anaconda3/bin/virtualenvwrapper.sh
and then reload profile by:
$ source ~/.bash_profile
I use an approach that I think is easy to maintain.
It also works well if you sometimes use Ubuntu systems, however I will be sure to address the OP's OSX requirement in my answer.
Create a .aliases file with your alias(es) in your home directory, e.g. ~/.aliases
Execute this file from your .bashrc file (this is executed each time for a new shell process) with source ~/.aliases. This is all you would actually need to do for Ubuntu btw.
On OSX call .bashrc from your ~/.profile file, i.e. have ~/.bash_profile contain: source ~/.bashrc

Resources