问题描述
我如何在YAML中逃脱一条线?我想要 json ='{title:travis_saulshanabrook_site,key:'$(cat〜/ .ssh / id_rsa.pub)'}'
在列表中,但是我无法将其解析成字符串。我可以把单引号放在整条线上,但是我将不得不逃避我的字符串中的每一个引号,使其很难阅读。字符串将作为中的bash命令运行 $ b $最好的解决方案是使用 |
指示符,使用 -
修饰符删除最终新队。这样就不需要额外的引用。
如果这个标量恰好是YAML文件中唯一使用的标签:
pre> |
json ='{title:travis_saulshanabrook_site,key:'$(cat〜/ .ssh / id_rsa.pub) $'
如果它是键的映射值 abc
:
abc:| -
json ='{title:travis_saulshanabrook_site关键:'$(cat〜/ .ssh / id_rsa.pub)'''
或者如果它是列表的一部分:
- | -
json ='{title:travis_saulshanabrook_site ,key:'$(cat〜/ .ssh / id_rsa.pub)'}'
How would I escape a whole line in YAML? I want to have json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}'
in a list, but I can't get it to parse into a string. I can put single quotes around the whole line, but then I would have to escape every single quote in my string, making it very hard to read. The string will be run as a bash command in Travis CI
解决方案 The most elegant solution is to use the literal style |
indicator, with the -
modifier to strip the final newline. That way there are no extra quotes necessary.
If this scalar happens to be the only thing in a YAML file use:
|-
json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}'
if it is a mapping value for key abc
:
abc: |-
json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}'
or if it is part of a list:
- |-
json='{"title": "travis_saulshanabrook_site","key": "'$(cat ~/.ssh/id_rsa.pub)'"}'
这篇关于引用YAML(对于Travis CI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!