ChatGPT解决这个技术问题 Extra ChatGPT

How to reload .bash_profile from the command line

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.


P
Peter Mortensen

Simply type source ~/.bash_profile.

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


How about alias BASHRELOAD=". ~/.bash_profile". If you do this often you can just alias it as br.
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 is your system loading the file? Some systems use other files, such as ~/.bashrc.
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"';
For people who forgot that you switched over to OhMyZsh. run open ~/.zshrc and make the changes there instead of your .bash_profile
C
Carl Norum
. ~/.bash_profile

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


Why does this work? Ie, what is the . command in this case?
the dot operator: . is simply an alias for the source command.
@GrahamPHeath - strictly speaking I think it's the other way around; the . is older than source is.
source is a bash specific implementation of .
@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."
j
jezrael

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.


P
Peter Mortensen

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.


you wont loose your history if you're using iterm2
j
jaypal singh

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

su - username

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.
you are opening another shell, this is not a reload you might as well open a new terminal or re log
A
Asclepius

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.


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)).
P
Peter Mortensen

Save the .bash_profile file Go to the user's home directory by typing cd Reload the profile with . .bash_profile


Just go to home with cd. No need for ~.
No need to cd - you can just reload it from the directory you're currently in: . ~/.bash_profile
P
Peter Mortensen

You just need to type . ~/.bash_profile.

Refer to What does 'source' do?.


P
Peter Mortensen

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

So you can call bashs the next time.


I alias that to reset -- easier to remember
P
Peter Mortensen

Use

alias reload!=". ~/.bash_profile"

Or if want to add logs via functions:

function reload! () {
    echo "Reloading bash profile...!"
    source ~/.bash_profile
    echo "Reloaded!!!"
}

No, its on yr preference. If wanna add some extra print lines showing status nor just go simply . ~/. bash_profile nor source ~/.bash_profile
P
Peter Mortensen

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.


P
Peter Mortensen

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;


C
Cassandra

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.


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 apparently just typing . .bash_profile while inside your home directory on Mac will do the job. Same as the reply given above by 7urkm3n.
For macOS, the default shell was changed to Z shell beginning with macOS v10.15 (Catalina) (2019).
P
Peter Mortensen

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.


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/…
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
t
tsujp

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