Fixes #11196: Add more examples for ruby heredoc shell scripts

This commit adds an additional example for how to preserve quotes in a
script that uses heredocs.
This commit is contained in:
Brian Cain 2019-11-15 09:24:58 -08:00
parent ed9769586b
commit 2adda6861e
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
1 changed files with 27 additions and 0 deletions

View File

@ -129,6 +129,33 @@ Vagrant.configure("2") do |config|
end end
``` ```
In the code block above, the script block starts with `<<-SCRIPT` and ends with `SCRIPT`.
This is known as a _Here Document_ or a _heredoc_. Additionally, if your script
relies on quotes and you do not wish for Ruby to escape your quotes, you may
want to use this style of _heredoc_ where `SCRIPT` is surrounded in single quotes:
```ruby
$script = <<-'SCRIPT'
echo "These are my \"quotes\"! I am provisioning my guest."
date > /etc/vagrant_provisioned_at
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: $script
end
```
Now that our _heredoc_ is quoted, our script will preserve the quotes in the string to `echo`:
```
==> default: Running provisioner: shell...
default: Running: inline script
default: These are my "quotes"! I am provisioning my guest.
```
For more examples of how to use _heredoc_ in Ruby, please refer to the
[Ruby documentation](https://ruby-doc.org/core-2.5.0/doc/syntax/literals_rdoc.html#label-Here+Documents).
It is understandable that if you are not familiar with Ruby, the above may seem very It is understandable that if you are not familiar with Ruby, the above may seem very
advanced or foreign. But do not fear, what it is doing is quite simple: advanced or foreign. But do not fear, what it is doing is quite simple:
the script is assigned to a global variable `$script`. This global variable the script is assigned to a global variable `$script`. This global variable