24

I set up some aliases in my .bash_profile on my Max OS X. It works but when I'm opening a new tab I always have to load my .bash_profile file with this command:

source ~/.bash_profile

How can I make it work for every terminal I'm opening, even if I'm restarting my Mac or Linux computer?

miguelmorin
  • 5,025
  • 4
  • 29
  • 64
Kevin
  • 4,823
  • 6
  • 36
  • 70
  • 2
    Try sourcing it from your ~/.bashrc (you may need to create that file). – John Zwinck Dec 18 '15 at 10:11
  • 2
    Add your stuff to `~/.profile`. Or add : `[[ -r ~/.bash_profile ]] && . ~/.bash_profile` in `~/.profile`. – P.P Dec 18 '15 at 10:17
  • @l3x If `~/.bash_profile` exists it is sourced before than `~/.profile` in default bash. –  Dec 18 '15 at 10:22
  • @l3x, what is the correct sequence of sourcing these init scripts? One may end up sourcing same files recursively... – anishsane Dec 18 '15 at 10:30
  • 3
    If you can configure your terminal to start a login shell, that should be sufficient. – glenn jackman Dec 18 '15 at 11:15
  • Thanks, this helped. – Kod Apr 22 '20 at 18:46
  • I encountered the same issue , as in @P.P s comment . I opened up the ~/.profile which was empty when opened an added the line [[ -r ~/.bash_profile ]] && . ~/.bash_profile then ran source ~/.profile in my terminal and started working fine. – Kod May 13 '20 at 14:39

4 Answers4

38

If you use zsh , you can add source ~/.bash_profile at the end of .zshrc file at the following path: /Users/YOUR USER NAME/.zshrc , then restart your Terminal/iTerm2 app.

Note that this file is hidden. You can press CMD + SHIFT + . in Finder to see it, Or just open it in default text editor with the following command from terminal:
open ~/.zshrc

Update

You don't need to do this by hand, run the following command:

echo "source ~/.bash_profile" >> ~/.zshrc

* Dont forget to restart your terminal.

Sabrina
  • 2,531
  • 1
  • 32
  • 30
23

The files executed at the start may be several, usually ~/.bashrc for interactive, non-login shells. The kind I assume you are using.

If so, create an ~/.bashrc file and source ~/.bash_profile in it:

if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

This web site has a lot of information about this.

Study this image, as it is a condensed guide

If you do need to find out exactly which file is being executed, take a look at this web page. It has a lot of specific tests to find out what file is setting what.

Specific for Mac-OS (which is an exception and loads ~/.bash_profile) do as is recomended in the site linked in this answer AFTER you confirm that your bash is doing as explained there.

Community
  • 1
  • 1
  • Import `.bashrc` from `.bash_profile`, not the other way around. A login shell is a special case of an interactive shell. If you are using `.profile` instead of `.bash_profile`, make a `.bash_profile` that simply imports `.profile`. – chepner Dec 18 '15 at 13:26
  • @chepner The user is asking for a way to load `~./bash_profile` instead of doing it manually. So, it could not be the other way around. The file `~./bash_profile` is not being loaded (acording to the user). –  Dec 18 '15 at 21:58
1

I know this is a pretty old post, but this problem comes and goes quite oftenly and a lot of laborous solutions are offered. The fact is: being aware of a simple info would solve this pretty fast and easy:

LINUX/Unix OS will load the profile files on startup following the rules below (some distros may have other files names, mainly for user local profiles, but the main rule follows this):

  1. Login Shell

First and foremost: /etc/profile is loaded (global settings); Next: ˜/.bash_profile (local user settings- other files may be found, like ˜/.profile, depending on the distro. Check documentation).

So, if you are in a Login Shell environment, put all your crazy stuff inside ˜/.bash_profile (or the file provided by your distro) and everything will be fine.

  1. Non-login Shell

First and foremost: /etc/bashrc (some distros will load bash.bashrc); The next file to be seeked and loaded is ˜/.bashrc

And that's why so many people (just like me) got frustrated having to source their ˜/.bash_profile every single time a Terminal was loaded. We were simply inserting info in the "wrong" file- regarding the kind of shell loaded (see above: login, non-login).

More details on variants of the files mentioned above and some info on login vs non-login shell can be found here.

Hope it helps \o/

jaymzleutz
  • 155
  • 2
  • 10
0

For Ubuntu system - Do it from Terminal > Preferences > Profile > Command > click on checkbox of Run command as a login shell and then reopen the Terminal

Screenshot of terminal preferences

helvete
  • 2,455
  • 13
  • 33
  • 37