1210

How can I reload file .bash_profile from the command line?

I can get the shell to recognize changes to .bash_profile by exiting and logging back in, but I would like to be able to do it on demand.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
markdorison
  • 139,374
  • 27
  • 55
  • 71

15 Answers15

2260

Simply type source ~/.bash_profile.

Alternatively, if you like saving keystrokes, you can type . ~/.bash_profile.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • 59
    How about `alias BASHRELOAD=". ~/.bash_profile"`. If you do this often you can just alias it as `br`. – bobobobo Apr 22 '13 at 18:56
  • 1
    any reason why I'd need to do this every single time/session? I can't get changes made to .bash_profile to persist even though they're there in the file when I open it in an editor. Confusing. – erwinheiser Sep 13 '14 at 13:22
  • 3
    @erwinheiser is your system loading the file? Some systems use other files, such as ~/.bashrc. – Graham P Heath Oct 31 '14 at 15:01
  • 2
    If you want to know if something went wrong on the load you can use: `alias reload='source ~/.bash_profile && echo "File .bash_profile reloaded correctly" || echo "Syntax error, could not import the file"';` – Cristian Batista Feb 08 '18 at 08:19
  • That's a really surprising metacharacter usage. What's the context where it is interpreted as source? I'm used to thinking of that as the current directory character. Is this a role it plays particularly in the global context of the `.bash_profile`? – jxramos Sep 04 '19 at 22:25
  • 4
    For people who forgot that you switched over to OhMyZsh. run open ~/.zshrc and make the changes there instead of your .bash_profile – Sankofa Dec 09 '19 at 21:29
  • Pro-tip: Do *not* accidentally run this with `~/.bash_history` as the filename. – JiK Feb 25 '20 at 22:48
  • 1
    This adds duplicates in my path. Isn't that an issue? – John Aug 28 '20 at 15:46
  • Do you use the same command for zsh? `source ~/.zprofile` ? – IamWarmduscher Nov 03 '20 at 14:46
134
. ~/.bash_profile

Just make sure you don't have any dependencies on the current state in there.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
36

Simply type:

. ~/.bash_profile

However, if you want to source it to run automatically when terminal starts instead of running it every time you open terminal, you might add . ~/.bash_profile to ~/.bashrc file.

Note:

When you open a terminal, the terminal starts bash in (non-login) interactive mode, which means it will source ~/.bashrc.

~/.bash_profile is only sourced by bash when started in interactive login mode. That is typically only when you login at the console (Ctrl+Alt+F1..F6), or connecting via ssh.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Mohammad Anini
  • 5,073
  • 4
  • 35
  • 46
25

If you don't mind losing the history of your current shell terminal, you could also do

bash -l

That would fork your shell and open up another child process of bash. The -l parameter tells Bash to run as a login shell. This is required, because .bash_profile will not run as a non-login shell. For more information about this, read here.

If you want to completely replace the current shell, you can also do:

exec bash -l

The above will not fork your current shell, but replace it completely, so when you type exit it will completely terminate, rather than dropping you to the previous shell.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ulukai
  • 1,360
  • 2
  • 12
  • 17
17

You can also use this command to reload the ~/.bash_profile for that user. Make sure to use the dash.

su - username
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • 7
    This will invoke an entire shell within a shell, far from ideal. The other options simply re-execute the relevant file, meaning they're (A) actually relevant to the asked question and (B) not piling up shells and possibly reloading other things that shouldn't be (env vars, etc.). There _are_ proper ways to replace the current shell outright (without nesting), but since that's off-topic, I'll leave interested readers to search elsewhere. – underscore_d Sep 24 '15 at 00:37
  • 4
    you are opening another shell, this is not a reload you might as well open a new terminal or re log – Juan Diego Nov 09 '15 at 17:23
15

I like the fact that after you have just edited the file, all you need to do is type:

. !$

This sources the file you had just edited in history. See What is bang dollar in bash.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
hyper_st8
  • 344
  • 7
  • 8
  • Perhaps explain that in your answer? ["`.`" is an alias for "`source`"](https://en.wikipedia.org/wiki/Dot_(command)#Source). On [the man page](https://linux.die.net/man/1/bash) it is near *"in the current shell environment and return the exit status of the last command executed from"* (though not that helpful (too terse)). – Peter Mortensen Jun 18 '22 at 19:07
13

You just need to type . ~/.bash_profile.

Refer to What does 'source' do?.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jenil Mewada
  • 575
  • 4
  • 14
10
  1. Save the .bash_profile file
  2. Go to the user's home directory by typing cd
  3. Reload the profile with . .bash_profile
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mithun Khatri
  • 636
  • 3
  • 9
  • 22
6

If the .bash_profile file does not exist, you can try to run the following command:

. ~/.bashrc

or

source ~/.bashrc

instead of .bash_profile.

You can find more information about bashrc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

Add alias bashs="source ~/.bash_profile" into your Bash file.

So you can call bashs the next time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shemeer M Ali
  • 1,030
  • 15
  • 39
4

Use

alias reload!=". ~/.bash_profile"

Or if want to add logs via functions:

function reload! () {
    echo "Reloading bash profile...!"
    source ~/.bash_profile
    echo "Reloaded!!!"
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
  • No, its on yr preference. If wanna add some extra print lines showing status nor just go simply `. ~/. bash_profile ` nor `source ~/.bash_profile` – 7urkm3n Mar 30 '18 at 18:06
3

While using source ~/.bash_profile or the previous answers works, one thing to mention is that this only reloads your Bash profile in the current tab or session you are viewing. If you wish to reload your bash profile on every tab/shell, you need to enter this command manually in each of them.

If you use iTerm, you can use CMD⌘ + Shift + I to enter a command into all current tabs. For terminal it may be useful to reference this issue;

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aug
  • 11,138
  • 9
  • 72
  • 93
2

I use Debian and I can simply type exec bash to achieve this. I can't say if it will work on all other distributions.

Cassandra
  • 284
  • 4
  • 18
  • 3
    This will not work in Mac (at least not in the version I am using - Sierra) because simply doing that executes a no login shell which does not run the .bash_profile – Ulukai Oct 23 '18 at 08:47
  • @Ulukai apparently just typing `. .bash_profile` while inside your home directory on Mac will do the job. Same as the reply given above by 7urkm3n. – Cassandra Oct 25 '18 at 00:00
  • For macOS, the default shell was changed to [Z shell](https://en.wikipedia.org/wiki/Z_shell) [beginning](https://en.wikipedia.org/wiki/MacOS_Catalina#Removed_or_changed_components) with [macOS v10.15](https://en.wikipedia.org/wiki/MacOS_Catalina) (Catalina) (2019). – Peter Mortensen Jun 18 '22 at 19:16
1

I am running macOS v10.12 (Sierra) and was working on this for a while (trying all recommended solutions). I became confounded, so I eventually tried restarting my computer! It worked.

My conclusion is that sometimes a hard reset is necessary.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3pitt
  • 899
  • 13
  • 21
  • 1
    Mike yes a hard reset will work because everything is then loaded freshly. As long as the changes you have made are functional, it will then take effect on next boot up. However it would be easier for you to dig around a little to find the command/method to just refresh the bash without having to do that all the time. There will be a way to achieve it without the reboot, which of course will soak up way too much time just to see if the latest change works! Perhaps have a look at http://osxdaily.com/2016/06/07/reload-bash_profile-zsh-profiles-command-line/ – Cassandra May 02 '17 at 06:56
  • 1
    yeah i tried both the abbreviated and full command to reload bash profile/path. it didn't work, only logging out and back in worked. weird – 3pitt May 02 '17 at 16:50
1

Simply re-sourcing the file won't "reload" in the sense that something is first unloaded, then loaded again. If that is what you want you can do:

hash -r && _SHOW_MESSAGES=1 exec -a -bash bash
tsujp
  • 1,066
  • 1
  • 12
  • 29