Why is `source /home/vagrant/.bashrc` not working in a Vagrant shell provisioning script?
Asked Answered
B

1

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?

Bollen answered 14/4, 2015 at 13:28 Comment(2)
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).Alice
Ugh, yes, you're right Etan, thanks. I didn't think of looking at the rest of the script.Bollen
B
12

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

Biblicist answered 14/4, 2015 at 13:51 Comment(3)
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.Bollen
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).Alice
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.Bollen

© 2022 - 2024 — McMap. All rights reserved.