# Using execute\_process when one of the arguments contains a space

**URL:** https://discourse.cmake.org/t/using-execute-process-when-one-of-the-arguments-contains-a-space/9864
**Category:** Code
**Created:** [January 16, 2024, 12:01pm UTC](https://discourse.cmake.org/t/using-execute-process-when-one-of-the-arguments-contains-a-space/9864 "2024-01-16T12:01:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [January 16, 2024, 12:01pm UTC](https://discourse.cmake.org/t/using-execute-process-when-one-of-the-arguments-contains-a-space/9864/1 "2024-01-16T12:01:16Z")

</div>

There is already [an entry with a similar title to what I’m trying to do](https://discourse.cmake.org/t/execute-command-and-arguments-that-contain-with-blank-spaces/807), but I’m afraid it doesn’t quite answer my question.

The situation is this: I’m trying to invoke execute\_process where one of the arguments contains a space. Specifically, I want to run bison with a set of flags which are specified in a string variable. For example,

```auto
execute_process(COMMAND win_flex_bison3-latest/win_bison.exe ${flags} -d -o ${nativeGenPath} ${nativeBisonFile}
                COMMAND_ECHO STDOUT)

```

…where ‘flags’ may be an empty string, or it may be a sequence of space-separated flags such as “-y -v”.

The exact content of ‘flags’ is supplied by the user at build time (and the file containing this command is generated at build time using ‘configure\_file’ and then ‘add\_custom\_target’), so I need to be able to handle various different cases.

What I find is that, if ‘flags’ is empty, of if it contains a single flag such as “-y”, then everything works. But if ‘flags’ is “-y -v” then here is the output that I see:

```auto
'win_bison.exe' '-y -v' '-d' '-o' 'C:\Users\name\Development\git\build\solutions\product\GENERATED\mathparse\parser\CalcParser.cpp' 'C:\Users\name\Development\git\apps\product\src\mathparse\parser\CalcParser.y'

C:/Users/name/Development/win_flex_bison3-latest/win_bison.exe: invalid option -- ' '

```

…indicating that it’s the embedded space between ‘-y’ and ‘v’ that’s the problem.

I’ve tried:

- The above, but putting ‘flags’ in quotes, i.e. “${flags}”

- putting the whole command line in quotes, i.e.

```auto
set(cmdstring "win_flex_bison3-latest/win_bison.exe ${flags} -d -o ${nativeGenPath} ${nativeBisonFile}")
execute_process(COMMAND ${cmdstring}
                COMMAND_ECHO STDOUT)

```

- recasting ‘flags’ as a list, i.e.

```auto
set(flags )
list(APPEND flags "-y")
list(APPEND flags "-v")

```

…but that just changes the error message to “invalid option – ’ ;'”

So I’m afraid I’ve run out of ideas. Is there any way of handling this situation?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 16, 2024, 1:23pm UTC](https://discourse.cmake.org/t/using-execute-process-when-one-of-the-arguments-contains-a-space/9864/2 "2024-01-16T13:23:02Z")

</div>

> [@EosPengwern](#):
>
> So I’m afraid I’ve run out of ideas. Is there any way of handling this situation?

I think you’re on the right track with the `list(APPEND flags)` strategy, but you also tried quoting things. How about:

```auto
set(flags -y -v) # or build up using `list(APPEND)`
execute_process(COMMAND win_flex_bison3-latest/win_bison.exe ${flags} -d -o ${nativeGenPath} ${nativeBisonFile}
                COMMAND_ECHO STDOUT)

```

---

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [January 16, 2024, 1:32pm UTC](https://discourse.cmake.org/t/using-execute-process-when-one-of-the-arguments-contains-a-space/9864/3 "2024-01-16T13:32:39Z")

</div>

Well, blow me down… Yes, that worked. The one permutation I hadn’t tried! I was thrown by the fact that the usual recommendation when passing lists is to enclose them in quotes, but clearly this is an exception to that rule.

Thank you.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 16, 2024, 1:58pm UTC](https://discourse.cmake.org/t/using-execute-process-when-one-of-the-arguments-contains-a-space/9864/4 "2024-01-16T13:58:16Z")

</div>

My rule is to quote everything except:

- when quoting would interfere with automatic dereferencing in conditionals
- when passing a list of built-up arguments to a command

There are exceptions like when one needs to smuggle lists with empty arguments through “higher-order” commands like `ExternalProject_add`.
