26

I have a .bash_profile file that I added some stuff to (aliases and color and some git stuff) and when I open iTerm and source it then everything's fine and I have everything but when I close iTerm and reopen it then no colors and no aliases until I source it again.

What should I do to permanently source it?

Is it maybe sourcing another file? How can I check that?

EDIT: I checked my iTerm preferences, and the "login shell" option is checked so I imagine it should source it when i open iTerm.

7
  • .bash_profile should be sourced automatically. Does it work with Terminal.app? In your iTerm settings, what is the Command being called under Preferences » Profiles » Default » General?
    – slhck
    Commented Mar 28, 2013 at 10:18
  • no command, 'login shell' is checked
    – levtatarov
    Commented Mar 28, 2013 at 10:24
  • Does it work in Terminal? Have you tried temporarily moving ~/Library/Preferences/com.googlecode.iterm2.plist somewhere else?
    – Lri
    Commented Apr 16, 2013 at 11:21
  • This page and the answers helped me with this issue, but also errors in your .bash_profile can cause code not to execute, in my case it was a [ character in an unescaped password string that stopped subsequent commands executing
    – chim
    Commented Nov 9, 2017 at 13:53
  • For me, I had to write source ~/.bash_profile in my ~/env.sh. This post basically helped me.
    – Sufian
    Commented May 30, 2018 at 11:08

6 Answers 6

30

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 either of these, open a new shell window, and your .bash_profile should be sourced.

2
  • 4
    chsh -s /bin/bash worked for me.
    – MiniGunnR
    Commented May 23, 2018 at 6:43
  • Definitely need the leading "/"
    – joehanna
    Commented Feb 6, 2021 at 3:07
9

Ok so I dug deeper into it and it's trying to source .profile and I, instead, had .bash_profile. So I created a ~/.profile file and copied the content of .bash_profile into it, and then - WORKS! It is sourced whenever I start iTerm or Terminal.

3
  • If both ~/.bash_profile and ~/.profile exist and are readable, bash should only execute ~/.bash_profile (see /usr/share/doc/bash/bash.html). Does ls -l ~/.bash_profile print something like -rw-r--r-- 1 username?
    – Lri
    Commented Apr 16, 2013 at 12:22
  • ls -l ~/.bash_profile prints -rw-r--r--@ 1 username staff ... date . Is the @ a problem? Commented Jun 26, 2014 at 13:22
  • I am having this same problem. And when I run that command I get the same thing as you Etienne Low Decarie.
    – wuno
    Commented Nov 11, 2016 at 5:55
4

.profile, .bash_profile and similar files are only sourced by "login" shells. In other words, only when you log in to the system. Therefore it does not make sense to set aliases there.

Keep only environment variables (export commands) in .bash_profile. Use ~/.bashrc for everything else. Source it from .bash_profile too.

2
  • 4
    OS X terminals launch login shells by default.
    – slhck
    Commented Mar 28, 2013 at 10:17
  • 1
    In my case, I am using mac zsh terminal, so I had to move the contents of my old .bash_profile to .zprofile, checkout this solution: superuser.com/a/187673/428141 that worked for me
    – s_bighead
    Commented Apr 23, 2020 at 18:03
1

I was thinking on .bash_profile then I forgot to login and doesn't work, if you login your .bash_profile will work perfectly.

Try:

$ login
$ login: (your name here)
$ password: (your password here)
1

.bash_profile is only sourced by bash on login shells. Any desktop environment is not a login shell. In bash it is NOT intended that .bash_profile or .profile get sourced when you open a Terminal window (in Linux, where a Terminal does not spawn a login shell).

So how and when are those files sourced? How would you solve this, for instance, in Xorg where .profile is also not sourced? I know this is not Linux, but I wanted to understand how other desktop environments deal with this.

On Xorg your .xsessionrc should actually source your .profile, because this file is sourced only once when your desktop launches. So this is the preferred solution, because you don't source your .profile unnecessary times.

With this it is also clear that .profile is intended as a place, where you define your persistent variables and they will probably never change and still propagate down to your terminal instances. Changing .profile would require you to relog your session.

So for Aqua (the desktop environment of macOS) it is only logical to do the same, but there is no equivalent to .xsessionrc. That's probably the reason why MacOS sources .zprofile when launching a terminal with zsh. It hasn't been sourced on the actual login of Aqua, so new Terminal windows spawn a login shell instead.

So, all you really have to do is change the default shell. My guess is that you just setup your Terminal to open bash via initial command. But this will not spawn a login shell and thus only .bashrc is sourced.

Just do chsh -s /bin/bash and reopen your Terminal. Now it will attempt to load the .bash_profile and if its missing, it will source the .profile. Please note that it, however, will not source .bashrc on login shells, so you want to put this at the end of your .bash_profile:

test ! -s "$HOME/.bashrc" || \. "$HOME/.bashrc"

Now having a .bash_profile will cause bash not to source .profile, so put also this in the top of your .bash_profile:

test ! -s "$HOME/.profile" || \. "$HOME/.profile"

I consider this to be the optimal solution, because is behaves like Apple decided it to behave for zsh. Login shells do spawn with Terminals and the desktop environment has nothing to source for the entire session. Other unix based distributions don't spawn login shells in their Terminals, but they offer a way to source something when the desktop environment launches.

It's not coherent, but it's important to understand the nuances. When you know where to put what and how to deal with it, you can essentially make a portable configuration. At least that's my motivation to switch back to bash. zsh is not the default shell for Linux distributions.

1
  • This also answers why when in a remote computer and activating bash, or any other shell, the *rc or *_profile will not be sourced. This is a very interesting answer to better understand and solve the problem in the most convenient way depending on any situation
    – 3nrique0
    Commented Jan 24 at 14:41
0

I had this same problem. I fixed it by going to iTerm > Preferences > General. Select your desired profile (if you have more than one) and toggle the Command option and input /usr/local/bin/bash -l. If you don't have Homebrew installed then it will likely be /bin/bash -l. If you aren't sure, type the command $which bash and it will tell which executable you are running. Moreover, if you are on OS X, I highly recommend learning why you want Homebrew and how to install it. Note that its L and not I depending on your font. By adding the flag -l it starts bash in login mode and NOT interactive mode. If it starts in login mode then it will source your .bash_profile. This worked for me, hopefully it will help you!

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .