Skip to content
Advertisement

Environment variables from .bashrc not being loaded when sourced from terraform script

I have a script being run with terraform on an EC2 instance like so:

provisioner "remote-exec" {
  inline = [
    "bash /path/to/myscript.sh
  ]
}

I have some environment variables defined in the .bashrc of this instance that I would like this script to have access to. However, inside that script I put,

source ~/.bashrc

as the first line, but the variables defined in .bashrc were still unset.

What do I need to do to successfully source .bashrc?

Advertisement

Answer

Many times, ~/.bashrc has a check to ensure that the settings are only applied in an interactive environment. At least this check is present by default in Ubuntu.

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

Within a script, you will not have PS1 set.

So, either remove/comment that line from your ~/.bashrc;

or source the script like this:

PS1=non-empty source ~/.bashrc
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement