[bash] How to reload .bash_profile from the command line?

How can I reload .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.

This question is related to bash shell command-line

The answer is


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.


you just need to type . ~/.bash_profile

refer: https://superuser.com/questions/46139/what-does-source-do


I wanted to post a quick answer that while using source ~/.bash_profile or the answers mentioned above 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;


  1. Save .bash_profile file
  2. Goto user's home directory by typing cd
  3. Reload the profile with . .bash_profile

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.


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 info 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.


if the .bash_profile does not exist you can try run the following command:

. ~/.bashrc 

or

 source ~/.bashrc

instead of .bash_profile. You can find more information about bashrc


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.


. ~/.bash_profile

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


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

my conclusion is that sometimes a hard reset is necessary


alias reload!=". ~/.bash_profile"

or if wanna add logs via functions

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

Add alias bashs="source ~/.bash_profile" in to your bash file. So you can call bashs from next time


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

su - username

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to shell

Comparing a variable with a string python not working when redirecting from bash script Get first line of a shell command's output How to run shell script file using nodejs? Run bash command on jenkins pipeline Way to create multiline comments in Bash? How to do multiline shell script in Ansible How to check if a file exists in a shell script How to check if an environment variable exists and get its value? Curl to return http status code along with the response docker entrypoint running bash script gets "permission denied"

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)