# Handling exit code bash variable in generated pipelines commands

**URL:** <https://forum.buildkite.community/t/handling-exit-code-bash-variable-in-generated-pipelines-commands/4334>\
**Category:** Pipelines\
**Created:** [April 4, 2025, 11:18am UTC](https://forum.buildkite.community/t/handling-exit-code-bash-variable-in-generated-pipelines-commands/4334 "2025-04-04T11:18:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zenogueira](https://avatars.discourse-cdn.com/v4/letter/z/e480ec/32.png) [@zenogueira](https://forum.buildkite.community/u/zenogueira)\
**Post date:** [April 4, 2025, 11:18am UTC](https://forum.buildkite.community/t/handling-exit-code-bash-variable-in-generated-pipelines-commands/4334/1 "2025-04-04T11:18:35Z")

</div>

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

```auto
 {
    "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](https://github.com/buildkite/agent/issues/1102#issuecomment-540330035) 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

---

<div class="post-metadata">

**Author:** ![zenogueira](https://avatars.discourse-cdn.com/v4/letter/z/e480ec/32.png) [@zenogueira](https://forum.buildkite.community/u/zenogueira)\
**Post date:** [April 4, 2025, 3:05pm UTC](https://forum.buildkite.community/t/handling-exit-code-bash-variable-in-generated-pipelines-commands/4334/2 "2025-04-04T15:05:28Z")

</div>

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

```auto
{
  "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 $?

---

<div class="post-metadata">

**Author:** ![Micheal](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/micheal/32/1588_2.png) [@Micheal](https://forum.buildkite.community/u/Micheal)\
**Post date:** [April 4, 2025, 4:53pm UTC](https://forum.buildkite.community/t/handling-exit-code-bash-variable-in-generated-pipelines-commands/4334/3 "2025-04-04T16:53:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/system/32/1321_2.png) [@system](https://forum.buildkite.community/u/system)\
**Post date:** [May 5, 2025, 2:54am UTC](https://forum.buildkite.community/t/handling-exit-code-bash-variable-in-generated-pipelines-commands/4334/4 "2025-05-05T02:54:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
