# List variable expanded differently when passed to a command

**URL:** <https://discourse.cmake.org/t/list-variable-expanded-differently-when-passed-to-a-command/4377>\
**Category:** Code\
**Created:** [October 31, 2021, 5:10pm UTC](https://discourse.cmake.org/t/list-variable-expanded-differently-when-passed-to-a-command/4377 "2021-10-31T17:10:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpanders](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/e19adc/32.png) [@gpanders](https://discourse.cmake.org/u/gpanders)\
**Post date:** [October 31, 2021, 5:10pm UTC](https://discourse.cmake.org/t/list-variable-expanded-differently-when-passed-to-a-command/4377/1 "2021-10-31T17:10:40Z")

</div>

Hi all.

Consider the following snippet:

```cmake
set(implicit_dirs ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES})

message(STATUS "implicit_dirs = ${implicit_dirs}")

add_custom_target(my_custom_target
    COMMAND ${CMAKE_COMMAND} -E env
        IMPLICIT_DIRS="${implicit_dirs}"
        some_command
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

```

I want to pass the semicolon separated list of directories in `${implicit_dirs}` to the env var `IMPLICIT_DIRS` for the external command `some_command`. The problem is that when the value is passed in `cmake -E env` the semicolons are being replaced by spaces, so the environment variable passing doesn’t work.

However, when I print `${implicit_dirs}` in the `message()` call it outputs as a semicolon separated list as expected.

How can I maintain the semicolon separation in the list when I pass it on to `cmake -E env`?

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [October 31, 2021, 8:27pm UTC](https://discourse.cmake.org/t/list-variable-expanded-differently-when-passed-to-a-command/4377/3 "2021-10-31T20:27:05Z")

</div>

The placement of your quotes assumes unix shell behavior, but CMake doesn’t work that way. You need to quote the whole `IMPLICIT_DIRS=...` part, not just the value to the right of the `=` character. The other missing piece is that you need to add the `VERBATIM` keyword. A working version would look like this:

```auto
add_custom_target(my_custom_target
    COMMAND ${CMAKE_COMMAND} -E env
        "IMPLICIT_DIRS=${implicit_dirs}"
        some_command
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    VERBATIM
)

```

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [October 31, 2021, 9:58pm UTC](https://discourse.cmake.org/t/list-variable-expanded-differently-when-passed-to-a-command/4377/4 "2021-10-31T21:58:54Z")

</div>

And you can also have a look at [cmake\_path(CONVERT … TO\_NATIVE\_PATH\_LIST …)](https://cmake.org/cmake/help/latest/command/cmake_path.html#convert) if you want a list in the platform format…
