0

I am trying to use 'source ~/.bash_profile' command from a shell script. But this doesn't work. My script is like this: test.sh

#!/bin/bash
source ~/.bash_profile

I use many ways to run the script ./test.sh, bash test.sh and sh test.sh. if run with . /test.sh it is work. This is the running the script? This is the right way to run the installation script?

btw I also test by changing script with . ~/.bash_profile, bash ~/.bash_profile, sh ~/.bash_profile, bash -c '~/.bash_profile'. What is the 'source' problem in shell script. Please explain me what is the right way? How can i source .bash_profile in the script. Thank to everyone!!

shreyasminocha
  • 550
  • 3
  • 14
Aung.mm
  • 19
  • 3
  • make sure the file is actually there: `ls -l ~/.bash_profile` – Oo.oO Dec 14 '18 at 12:03
  • 2
    @shreyasminocha Probably not worth submitting edits on questions which are going to be deleted anyway. You are creating more noise by bumping this back onto the start page. – tripleee Dec 14 '18 at 15:22

2 Answers2

0

If I understand correctly you have not changed the access to make the script executable?

   chmod +x test.sh

Only then can you run it. Hope this helps (Let me know if I misunderstood)

0

When you start a bash script you are starting a new shell, so you are running source in a subshell and not your shell where you are issuing command.

It works when you start it with . /test.sh because that is the same as doing source /test.sh you are starting the commands from script in the same shell and not in a subshell.

ralz
  • 493
  • 2
  • 8