Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

vagrant

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?

like image 334
Phil Gyford Avatar asked Apr 14 '15 13:04

Phil Gyford


People also ask

What is shell provisioning in Vagrant?

Shell provisioning is ideal for users new to Vagrant who want to get up and running quickly and provides a strong alternative for users who are not comfortable with a full configuration management system such as Chef or Puppet. For POSIX-like machines, the shell provisioner executes scripts with SSH.

Why can't I run bashrc from bash_profile?

As bash_profile runs before bashrc is picked up, if you don't have a source command in bash_profile, bashrc won't run. There are two ways to tackle this, either delete bash_profile or if you use bash_profile, just add the following line anywhere in the file Highly active question.

Does Bash_env need to be in ~/.bashrc or ~/.profile?

From an interactive shell session, the BASH_ENV variable does not have to be set in this way, and the script does not need to source ~/.bashrc nor ~/.profile since these have already been sourced.

How do I run a bash script in Vagrant?

Additionally, if you are running Vagrant on something like Cygwin or WSL where bash is available, then you should be able to use an extension like ".sh". To run a script already available on the guest you can use an inline script to invoke the remote script on the guest. You can parameterize your scripts as well like any normal shell script.


1 Answers

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

like image 186
Tom Stuart Avatar answered Oct 24 '22 16:10

Tom Stuart