rubybashvagrantvagrantfilevagrant-windows

Vagrantfile: How do I pass a host environment variable with an unescaped dollar sign in it?


If I add something like this to Vagrantfile in the appropriate place:

vm.provision "shell",
path: "my_node.sh",
env: {"MY_VARIABLE"=>ENV['CONTAINS_A_DOLLAR_SIGN']}

And in my my_node.sh I add this:

echo "$MY_VARIABLE"

If my CONTAINS_A_DOLLAR_SIGN environmental variable is before$after my vagrant up output will contain before but not $after.

How do I fix this in Vagrantfile?


Solution

  • With a hint from @matt-schuchard, I added this at the top of Vagrantfile:

    require 'shellwords'
    

    and changed

    env: {"MY_VARIABLE"=>ENV['CONTAINS_A_DOLLAR_SIGN']}
    

    to

    env: {"MY_VARIABLE"=>Shellwords.escape(ENV['CONTAINS_A_DOLLAR_SIGN'])}
    

    Now vagrant up outputs before$after not just before!