# Preventing the contents of a string being substituted

**URL:** <https://discourse.cmake.org/t/preventing-the-contents-of-a-string-being-substituted/11752>\
**Category:** Code\
**Created:** [September 23, 2024, 1:25pm UTC](https://discourse.cmake.org/t/preventing-the-contents-of-a-string-being-substituted/11752 "2024-09-23T13:25:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![h-2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/h-2/32/4914_2.png) [@h-2](https://discourse.cmake.org/u/h-2)\
**Post date:** [September 23, 2024, 1:25pm UTC](https://discourse.cmake.org/t/preventing-the-contents-of-a-string-being-substituted/11752/1 "2024-09-23T13:25:41Z")

</div>

I want to run awk with a script as argument. This is what I have done

```auto
set(AWK_SCRIPT [=[$1 == "TOTAL" && $4 == "100%" { exit 0; }; END { print "Line coverage is not 100%. Please see the HTML coverage report."; exit 1; }]=])

message(STATUS ${AWK_SCRIPT})

add_custom_target(coverage_check
                  DEPENDS ${PROJECT_BINARY_DIR}/summary.txt
                  COMMAND awk ${AWK_SCRIPT} ${PROJECT_BINARY_DIR}/summary.txt
                  COMMENT "Check whether line coverage is 100%.")

```

I create the script as a raw string. This works. When I print the string, everything is shown correctly ($ is preserved, spaces…). But when running this, I get

```auto
... && /bin/sh -c awk\ \ ==\ "TOTAL"\ &&\ \ ==\ "100%"\ {\ exit\ 0;\ };\ END\ {\ print\ "Line\ coverage\ is\ not\ 100%.\ Please\ see\ the\ HTML\ coverage\ report.";\ exit\ 1;\ }\ /home/hannes/devel/tools-build/coverage/summary.txt
awk: line 1: syntax error at or near ==
/bin/sh: 1: }: not found
/bin/sh: 1: END { print Line\ coverage\ is\ not\ 100%.\ Please\ see\ the\ HTML\ coverage\ report.: not found
/bin/sh: 1: exit 1: not found
/bin/sh: 1: } /home/hannes/devel/tools-build/coverage/summary.txt: not found
ninja: build stopped: subcommand failed.

```

Why is the string variable split into multiple arguments to the command? What’s up with all the escaping? How can I pass the variable “as-is” to the command as a single argument?

Thank you

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [September 23, 2024, 2:06pm UTC](https://discourse.cmake.org/t/preventing-the-contents-of-a-string-being-substituted/11752/2 "2024-09-23T14:06:43Z")

</div>

Try adding `VERBATIM` to the arguments for `add_custom_target()`. Not using `VERBATIM` is effectively never the right choice, and it’ recommended to make it a habit to use it always. I cannot guarantee that it will fix your issue, but it’s quite likely that it will.

---

<div class="post-metadata">

**Author:** ![h-2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/h-2/32/4914_2.png) [@h-2](https://discourse.cmake.org/u/h-2)\
**Post date:** [September 23, 2024, 2:16pm UTC](https://discourse.cmake.org/t/preventing-the-contents-of-a-string-being-substituted/11752/3 "2024-09-23T14:16:48Z")

</div>

Thanks. This generates a different error:

```auto
FAILED: CMakeFiles/coverage_check /home/hannes/devel/tools-build/coverage/CMakeFiles/coverage_check 
cd /home/hannes/devel/tools-build/coverage && awk "\$1 == \"TOTAL\" && \$4 == \"100%\" { exit 0" " }" " END { print \"Line coverage is not 100%. Please see the HTML coverage report.\"" " exit 1" " }" /home/hannes/devel/tools-build/coverage/summary.txt
awk: line 2: missing } near end of file

```

This looks a little better (maybe?), but obviously some funky things are still happening. Look at the quotes after exit.

Also, the semi-colons are all gone. But now that I double-checked, I see that they are also gone from the variable (at least not visible when printing it).

I really just want to pass a string “as-is” without any logic happening. Surely, it can’t be so difficult?

---

<div class="post-metadata">

**Author:** ![h-2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/h-2/32/4914_2.png) [@h-2](https://discourse.cmake.org/u/h-2)\
**Post date:** [September 23, 2024, 2:23pm UTC](https://discourse.cmake.org/t/preventing-the-contents-of-a-string-being-substituted/11752/4 "2024-09-23T14:23:40Z")

</div>

Ok, if I additionally escape the semicolons in the raw string, it works. Not sure why the raw string isn’t raw, but oh well.

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [September 23, 2024, 2:31pm UTC](https://discourse.cmake.org/t/preventing-the-contents-of-a-string-being-substituted/11752/5 "2024-09-23T14:31:11Z")

</div>

It is, but semi-colon is also a CMake argument separator, and that is what is “eating” them. It should be fixed by adding quotes around the string’s expansion:

```auto
    COMMAND awk ${AWK_SCRIPT} ${PROJECT_BINARY_DIR}/summary.txt

```
