如何在yaml中正确转义&&
How to escape && in yaml correctly
我正在尝试 运行 我的 gitlab 管道中的以下行:
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
我在 Ubuntu 终端中测试了命令,它在那里工作正常。
我在我的 gitlab-ci.yml 文件中尝试了以下行但没有成功:
script:
- "[ -s \"$NVM_DIR/nvm.sh\" ] && \. \"$NVM_DIR/nvm.sh\"" # escaping the full line with double quotation marks and then only escaping the quotation marks inside the line -> is marked as invalid yml by gitlab: " found unknown escape character while parsing a quoted scalar"
- \[ -s \"$NVM_DIR/nvm.sh\" ] && \. \"$NVM_DIR/nvm.sh\" # escaping each special character and replacing the ampersands with & -> fails with: syntax error: unexpected "&"
- "[ -s \"$NVM_DIR/nvm.sh\" ] && \. \"$NVM_DIR/nvm.sh\"" # this was marked as invalid yml syntax by gitlab ("found unknown escape character while parsing a quoted scalar")
阅读 Biffen 的评论后,以下行有效:
- "[ -s \"$NVM_DIR/nvm.sh\" ] && . \"$NVM_DIR/nvm.sh\""
所以问题出在转义上。不再需要转义,因为我将命令用双引号引起来。
我正在尝试 运行 我的 gitlab 管道中的以下行:
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
我在 Ubuntu 终端中测试了命令,它在那里工作正常。
我在我的 gitlab-ci.yml 文件中尝试了以下行但没有成功:
script:
- "[ -s \"$NVM_DIR/nvm.sh\" ] && \. \"$NVM_DIR/nvm.sh\"" # escaping the full line with double quotation marks and then only escaping the quotation marks inside the line -> is marked as invalid yml by gitlab: " found unknown escape character while parsing a quoted scalar"
- \[ -s \"$NVM_DIR/nvm.sh\" ] && \. \"$NVM_DIR/nvm.sh\" # escaping each special character and replacing the ampersands with & -> fails with: syntax error: unexpected "&"
- "[ -s \"$NVM_DIR/nvm.sh\" ] && \. \"$NVM_DIR/nvm.sh\"" # this was marked as invalid yml syntax by gitlab ("found unknown escape character while parsing a quoted scalar")
阅读 Biffen 的评论后,以下行有效:
- "[ -s \"$NVM_DIR/nvm.sh\" ] && . \"$NVM_DIR/nvm.sh\""
所以问题出在转义上。不再需要转义,因为我将命令用双引号引起来。