How to reload .bash_profile from the command line
Asked Answered
I

15

1222

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.

Issacissachar answered 5/1, 2011 at 19:9 Comment(0)
R
2278

Simply type source ~/.bash_profile.

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

Resonate answered 5/1, 2011 at 19:10 Comment(9)
How about alias BASHRELOAD=". ~/.bash_profile". If you do this often you can just alias it as br.Baa
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.Mccabe
@Mccabe is your system loading the file? Some systems use other files, such as ~/.bashrc.Viscountess
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"';Nature
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?Hexone
For people who forgot that you switched over to OhMyZsh. run open ~/.zshrc and make the changes there instead of your .bash_profileCampney
Pro-tip: Do not accidentally run this with ~/.bash_history as the filename.Wildman
This adds duplicates in my path. Isn't that an issue?Runny
Do you use the same command for zsh? source ~/.zprofile ?Goldner
R
134
. ~/.bash_profile

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

Ronnironnica answered 5/1, 2011 at 19:11 Comment(8)
Why does this work? Ie, what is the . command in this case?Suite
the dot operator: . is simply an alias for the source command.Viscountess
@GrahamPHeath - strictly speaking I think it's the other way around; the . is older than source is.Ronnironnica
source is a bash specific implementation of .Ratepayer
So, . is an alias or is source a specific implementation?Anabantid
@StasS - . and source are literally the same thing in bash. From the link: "source is a synonym for dot/period '.' in bash, but not in POSIX sh, so for maximum compatibility use the period."Ronnironnica
This method isn't guaranteed to work or to be clean because .bash_profile is not guaranteed to be idempotent. For a cleaner method then you really want to start a new bash process.Parchment
i like this simplified answer :)Molini
N
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.

Neurath answered 10/5, 2015 at 19:44 Comment(0)
D
26

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.

Dither answered 23/10, 2018 at 8:42 Comment(1)
you wont loose your history if you're using iterm2Derive
O
17

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

su - username
Overwind answered 19/9, 2012 at 23:33 Comment(2)
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.Wick
you are opening another shell, this is not a reload you might as well open a new terminal or re logPhilan
A
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.

Antigone answered 8/11, 2016 at 14:45 Comment(1)
Perhaps explain that in your answer? "." is an alias for "source". On the man page 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)).K
S
15

You just need to type . ~/.bash_profile.

Refer to What does 'source' do?.

Sanctitude answered 18/8, 2018 at 7:34 Comment(0)
D
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
Darby answered 7/5, 2014 at 10:15 Comment(2)
Just go to home with cd. No need for ~.Irresolution
No need to cd - you can just reload it from the directory you're currently in: . ~/.bash_profileKed
D
8

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.

Directions answered 12/8, 2016 at 19:22 Comment(0)
M
4

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

So you can call bashs the next time.

Motteo answered 10/9, 2015 at 13:12 Comment(1)
I alias that to reset -- easier to rememberCobby
B
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!!!"
}
Buonarroti answered 6/2, 2018 at 11:47 Comment(1)
No, its on yr preference. If wanna add some extra print lines showing status nor just go simply . ~/. bash_profile nor source ~/.bash_profileBuonarroti
C
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;

Chaetopod answered 19/5, 2017 at 21:14 Comment(0)
P
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.

Panarabism answered 29/3, 2017 at 2:50 Comment(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_profileDither
@Dither apparently just typing . .bash_profile while inside your home directory on Mac will do the job. Same as the reply given above by 7urkm3n.Panarabism
For macOS, the default shell was changed to Z shell beginning with macOS v10.15 (Catalina) (2019).K
H
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.

Heteroplasty answered 26/4, 2017 at 15:13 Comment(2)
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 osxdaily.com/2016/06/07/…Panarabism
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. weirdHeteroplasty
R
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
Rectitude answered 27/6, 2022 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.