Handling exit code bash variable in generated pipelines commands

Hello,

We’re generating a pipeline which needs to handle an exit code in the middle of a step’s command. We have multiple commands chained and one of them runs puppet, which can exit with code 2 and still mean a successful run.

Example step

 {
    "label": "puppet",
    "command": "sudo puppet agent -t || [[ $? -eq 2 ]] && next-command"
  }

When we run the pipeline upload we get this error:

fatal: pipeline interpolation of “(stdin)” failed: interpolating command: Expected identifier to start with a letter, got ?

I’ve tried escaping the $ with \ and $$ (as per some indicated in gh and docs) and neither works.

I also tried \$$ but that’s just mixing things randomly (and ofc doesn’t work).

Do you have any special escaping syntax for this situation? Should I do something differently?
Ideally I don’t want to have to create a script for this step to call.

Thanks for your attention

We ended up creating a script for this step, but in the solution for this case is to doube escape:

{
  "label": "puppet",
  "command": "sudo puppet agent -t || [[ \\$? -eq 2 ]] && next-command"
}

explanation:

  • In JSON, to get a literal backslash, you need to escape it: \\.
  • So \\$? in JSON becomes \$? in the shell.
  • And then at runtime, the shell interprets \$? as $?

Hi @zenogueira yes you are correct. In this case \$? in JSON becomes $? in the shell and then at run time, the shell interprets $? as $?