Merge pull request #11198 from briancain/docs/add-quote-heredoc-example

Fixes #11196: Add more examples for ruby heredoc shell scripts
This commit is contained in:
Brian Cain 2019-11-15 14:29:53 -08:00 committed by GitHub
commit 51ceda7e25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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