154

I'm new to bash shell scripting, and have come across a challenge. I know I can reload my ".profile" file by just doing:

. .profile

but I'm trying to execute the same in a bash script I'm writing and it is just not working. Any ideas? Anything else I can provide to clarify?

Thanks

3
  • As Ignacio points out below, your script is running in a subshell. The subshell can't modify the main shell (the child process can't modify its parent). So you need to "source" the script by using the "." command (which can also be spelled as "source"). So if your script wants to, say, modify environment variables, you need to do something like "source myscript" or ". myscript" (they both mean the same thing). This will modify your main shell's environment. (Which I think is what you're trying to do, let me know if this is wrong.) Commented Jul 20, 2010 at 3:08
  • 1
    I got my answer from your question :) Commented Oct 18, 2016 at 17:04
  • Both` source .bash_profile` and . ./.bash_profile returns "bash: .: Is a directory" Commented Jul 13, 2023 at 15:12

5 Answers 5

256

Try this to reload your current shell:

source ~/.profile
Sign up to request clarification or add additional context in comments.

1 Comment

Do you have an alternate solution ? Because using the source command will run the file as a script... In worst cases, if somebody would use a variable assignment like MyVar="$foo$MyVar" in their bash_profile, then source ~/.profile would give the end result MyVar="$foo$MyVar$MyVar", hence $MyVar would have the wrong value afterwards. (Regardless of bad practices, just ask for an alternate solution)
13

A couple of issues arise when trying to reload/source ~/.profile file. [This refers to Ubuntu linux - in some cases the details of the commands will be different]

  1. Are you running this directly in terminal or in a script?
  2. How do you run this in a script?

Ad. 1)

Running this directly in terminal means that there will be no subshell created. So you can use either two commands:

source ~/.bash_profile

or

. ~/.bash_profile

In both cases this will update the environment with the contents of .profile file.

Ad 2) You can start any bash script either by calling

sh myscript.sh 

or

. myscript.sh

In the first case this will create a subshell that will not affect the environment variables of your system and they will be visible only to the subshell process. After finishing the subshell command none of the exports etc. will not be applied. THIS IS A COMMON MISTAKE AND CAUSES A LOT OF DEVELOPERS TO LOSE A LOT OF TIME.

In order for your changes applied in your script to have effect for the global environment the script has to be run with

.myscript.sh

command.

In order to make sure that you script is not runned in a subshel you can use this function. (Again example is for Ubuntu shell)

#/bin/bash

preventSubshell(){
  if [[ $_ != $0 ]]
  then
    echo "Script is being sourced"
  else
    echo "Script is a subshell - please run the script by invoking . script.sh command";
    exit 1;
  fi
}

I hope this clears some of the common misunderstandings! :D Good Luck!

Comments

11

Try this:

cd 
source .bash_profile

1 Comment

returns "bash: .: Is a directory"
4

The bash script runs in a separate subshell. In order to make this work you will need to source this other script as well.

3 Comments

I'm not sure (still new to all of this) what you exactly mean by "source"-ing the other script. Could please expand a little on that. However I have tried: $ . ~/.profile $ . /etc/profile with no success. Thank you so much.
@amirrustan: Your script will need to source your .profile file something like this: . $HOME/.profile and you will need to start your script by sourcing it also. Something like . /path/to/yourscript
@amirrustam please read superuser.com/questions/176783/…
2

Try:

#!/bin/bash
# .... some previous code ...
# help set exec | less
set -- 1 2 3 4 5  # fake command line arguments
exec bash --login -c '
echo $0
echo $@
echo my script continues here
' arg0 "$@"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.