# Pass double quotes to batch script with execute\_process()

**URL:** <https://discourse.cmake.org/t/pass-double-quotes-to-batch-script-with-execute-process/11198>\
**Category:** Code\
**Created:** [July 8, 2024, 9:56pm UTC](https://discourse.cmake.org/t/pass-double-quotes-to-batch-script-with-execute-process/11198 "2024-07-08T21:56:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zeranoe](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zeranoe/32/4741_2.png) [@Zeranoe](https://discourse.cmake.org/u/Zeranoe)\
**Post date:** [July 8, 2024, 9:56pm UTC](https://discourse.cmake.org/t/pass-double-quotes-to-batch-script-with-execute-process/11198/1 "2024-07-08T21:56:18Z")

</div>

I’m trying to execute a batch script with an argument that requires double quotes, something like `script.bat "foo=bar"`. If this argument is passed in without the double quotes, you end up with %1 being “foo” and %2 being “bar”, instead of the desired %1 being “foo=bar”.

With single double quotes around the argument (`execute_process(COMMAND script.bat "foo=bar")`) I get no quotes in the call. All other combinations of escaping seem to propagate the escape to the call itself, with backslashes in the arg.

Is this possible without writing an intermediary batch script to call this other batch script?

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [July 9, 2024, 8:06pm UTC](https://discourse.cmake.org/t/pass-double-quotes-to-batch-script-with-execute-process/11198/2 "2024-07-09T20:06:15Z")

</div>

Your example just has a CMake string defined, without any quotes.  
CMake just allows you to omit the `"` around string, as long as you don’t have whitespaces or semicolons as content.

Have you tried this: `execute_process(COMMAND script.bat "\"foo=bar\"")`  
Or if there is no variable involved: `execute_process(COMMAND script.bat [["foo=bar"]])`

Craig Scott wrote a blog article, that might help you in understanding quoting:

> **[Quoting In CMake](https://crascit.com/2022/01/25/quoting-in-cmake/)**
>
> Get some clarity on CMake's quoting rules, as we highlight common pitfalls and offer guidelines for safer use.

---

<div class="post-metadata">

**Author:** ![Zeranoe](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zeranoe/32/4741_2.png) [@Zeranoe](https://discourse.cmake.org/u/Zeranoe)\
**Post date:** [July 9, 2024, 10:56pm UTC](https://discourse.cmake.org/t/pass-double-quotes-to-batch-script-with-execute-process/11198/3 "2024-07-09T22:56:50Z")

</div>

Both of these escape options result in the batch script being called with `"\"foo=bar\""`, the desired result is `"foo=bar"`.

Here is a complete example that demonstrates the problem:

```auto
cmake_minimum_required(VERSION 3.29)

project(quotes)

file(WRITE quotes.bat "echo %2")

# will print "bar", should print nothing
execute_process(COMMAND quotes.bat "foo=bar")

```

The output for this is `bar`, which is not the correct invocation of quotes.bat. The correct call to script.bat should be `script.bat "foo=bar"`.

I need to invoke a batch script with an argument that contains verbatim double quotes surrounding an arg.

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [July 23, 2024, 5:03pm UTC](https://discourse.cmake.org/t/pass-double-quotes-to-batch-script-with-execute-process/11198/4 "2024-07-23T17:03:03Z")

</div>

`execute_process` assumes that the child uses the MSVC C runtime library’s [CommandLineToArgvW](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw) to parse the command line. In order to do anything different, a new option will be needed.

[CMake Issue 16321](https://gitlab.kitware.com/cmake/cmake/-/issues/16321) requests a way to control the raw command line on Windows.
