Search code examples
bashdot-source

`source ~/.bash_profile` do not works inside a bash script


This is a script that move a bash file into homepage and load it with source command.

# update.sh
#!/bin/bash
cp -f $PWD/bash_profile ~/.bash_profile
source ~/.bash_profile

It does not work! It update file with cp -f $PWD/bash_profile ~/.bash_profile.

Inside ~/.bash_profile there is a new PS1 definition. File is updated but no changes happened until new window is opened. I need to run source ~/.bash_profile after script execution ...

Is it possibile to run source command inside bash script?


Solution

  • From MangeshBiradar here:

    Execute Shell Script Using . ./ (dot space dot slash)

    While executing the shell script using “dot space dot slash”, as shown below, it will execute the script in the current shell without forking a sub shell.

    $ . ./setup.bash

    In other words, this executes the commands specified in the setup.bash in the current shell, and prepares the environment for you.