10

I'm having problems with a shell provisioning script used by Vagrant, as it's not executing source /home/vagrant/.bashrc. I've reduced the problem down to this...

Within my VM I have a file at /home/vagrant/testfile that contains this:

echo "In testfile"

And at the end of /home/vagrant/.bashrc I have this:

echo "In .bashrc"

Both files are owned by the vagrant user.

In one of my Vagrant provisioning shell scripts I have this:

echo "Hello"
source /home/vagrant/testfile
source /home/vagrant/.bashrc
echo "Goodbye"

Running vagrant provision gives this:

Hello
In testfile
Goodbye

When I do vagrant ssh then /home/vagrant/.bashrc is run as usual, and I automatically see:

In .bashrc

So why does doing source /home/vagrant/.bashrc have no effect from within my provisioning script?

Community
  • 1
  • 1
Phil Gyford
  • 13,432
  • 14
  • 81
  • 143
  • What does the *rest* of your `.bashrc` do? I'm betting there's a test for an interactive session near the top (a check on `PS1` or a check on the value of `$-` or similar). – Etan Reisner Apr 14 '15 at 13:42
  • Ugh, yes, you're right Etan, thanks. I didn't think of looking at the rest of the script. – Phil Gyford Apr 14 '15 at 13:53

1 Answers1

13

You need to remove the “exit if not running interactively” bit (e.g. [ -z "$PS1" ] && return) from the top of your .bashrc.

Tom Stuart
  • 228
  • 3
  • 6
  • Thank you! Actually looking at what else was in `.bashrc` was the one bit of narrowing down this problem I didn't think of doing. Yes, there was a bit that was at the top of the file, not put there by me, which prevented it doing anything when run like this. – Phil Gyford Apr 14 '15 at 13:52
  • 1
    Do *not* remove that. It is important for all sorts of things that normally go in that file. Anything that needs to run even when that isn't true likely shouldn't be in that file (or if they are should be above that bit). – Etan Reisner Apr 14 '15 at 15:30
  • 1
    Oh, I'm not going to remove it, but thanks for the warning. Now I know why this is happening I can find other ways around my original problem. It was really bugging me that something was quietly failing and I had no idea why! Now I know. – Phil Gyford Apr 14 '15 at 15:55