Boommeister
Boommeister

Reputation: 2107

How to escape && in yaml correctly

I'm trying to run following line in my gitlab pipeline:

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

I tested the command in a Ubuntu terminal and it works fine there.

I tried following lines in my gitlab-ci.yml file but without success:

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")

Upvotes: 6

Views: 3707

Answers (1)

Boommeister
Boommeister

Reputation: 2107

Following line worked after reading Biffen's comment:

- "[ -s \"$NVM_DIR/nvm.sh\" ] && . \"$NVM_DIR/nvm.sh\""

So the problem was the escaped . that didn't need to be escaped anymore since I wrapped the command in double quotes.

Upvotes: 6

Related Questions