# file(WRITE ...) behaves differently on Linux compared to Windows regarding escaped whitespaces?

**URL:** <https://discourse.cmake.org/t/file-write-behaves-differently-on-linux-compared-to-windows-regarding-escaped-whitespaces/6012>\
**Category:** Code\
**Tags:** os:linux, os:windows\
**Created:** [July 6, 2022, 11:31am UTC](https://discourse.cmake.org/t/file-write-behaves-differently-on-linux-compared-to-windows-regarding-escaped-whitespaces/6012 "2022-07-06T11:31:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohannesWilde](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@JohannesWilde](https://discourse.cmake.org/u/JohannesWilde)\
**Post date:** [July 6, 2022, 11:31am UTC](https://discourse.cmake.org/t/file-write-behaves-differently-on-linux-compared-to-windows-regarding-escaped-whitespaces/6012/1 "2022-07-06T11:31:29Z")

</div>

Hi,

I wanted to automate the version-string generation from a git tag via CMake and came up with an approach very similar to the following.

main.c:

```auto
#include <stdio.h>

int main()
{
    printf("Hello World!\n");
    return 0;
}

```

CMakeLists.txt:

```auto
cmake_minimum_required(VERSION 3.5)

project(TestingCmake LANGUAGES C)

add_executable(TestingCmake main.c)

set(FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/test_file.c)

add_custom_command(
    OUTPUT ${FILE_NAME}
    COMMAND ${CMAKE_COMMAND}
        -DSTRING_CONTENT="text with whitespace"
        -DFILE_PATH="${FILE_NAME}"
            -P ${CMAKE_CURRENT_SOURCE_DIR}/CreateVersion.cmake
)

add_custom_target(CreateClControllerVersionFile ALL
    COMMAND ${CMAKE_COMMAND}
        -DSTRING_CONTENT="text with whitespace"
        -DFILE_PATH="${FILE_NAME}"
            -P ${CMAKE_CURRENT_SOURCE_DIR}/CreateVersion.cmake
)

add_dependencies(TestingCmake CreateClControllerVersionFile)

```

CreateVersion.cmake:

```auto
file(WRITE ${FILE_PATH} "${STRING_CONTENT}")

```

Doing so I encountered [to me] seemingly strange behavior:  
- under Windows [CMake 3.23.2] I got the expected result in the output file

```auto
text with whitespace

```

```
- under Linux [CMake 3.21.3] I encountered

```

```auto
text\ with\ whitespace

```

Is this expected behavior?

Is there a way for me to tell CMake to unescape the whitespaces during writing to file?

Should I approach my goal in a totally different way?

Thankful for any help

Johannes

---

<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:** [July 6, 2022, 11:47am UTC](https://discourse.cmake.org/t/file-write-behaves-differently-on-linux-compared-to-windows-regarding-escaped-whitespaces/6012/2 "2022-07-06T11:47:00Z")

</div>

You may want the `VERBATIM` argument here. Another problem (which may avoid the issue) is that inner quotes don’t work in CMake.

> [@JohannesWilde](#):
>
> `-DSTRING_CONTENT="text with whitespace"`

Try `"-DSTRING_CONTENT=text with whitespace"`; that may avoid the need for the `VERBATIM`. Also the same fix for `FILE_PATH`.

---

<div class="post-metadata">

**Author:** ![JohannesWilde](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@JohannesWilde](https://discourse.cmake.org/u/JohannesWilde)\
**Post date:** [July 6, 2022, 2:22pm UTC](https://discourse.cmake.org/t/file-write-behaves-differently-on-linux-compared-to-windows-regarding-escaped-whitespaces/6012/3 "2022-07-06T14:22:54Z")

</div>

Hi Ben,

thanks for the fast reply.

Your proposed solution with the surrounding quotes seems to work. Thanks. Could you hint me to somewhere, in order to understand why?

In addition I ended up using `VERBATIM` as well, because I also had `(` and `)` as part of the string - which needed to be escaped for the command line.

Thanks

Johannes

---

<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:** [July 6, 2022, 9:32pm UTC](https://discourse.cmake.org/t/file-write-behaves-differently-on-linux-compared-to-windows-regarding-escaped-whitespaces/6012/4 "2022-07-06T21:32:10Z")

</div>

> [@JohannesWilde](#):
>
> Your proposed solution with the surrounding quotes seems to work. Thanks. Could you hint me to somewhere, in order to understand why?

My [Quoting In CMake](https://crascit.com/2022/01/25/quoting-in-cmake/) article discusses the broader topic. It may fill in a few blanks for 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:** [July 9, 2022, 1:16pm UTC](https://discourse.cmake.org/t/file-write-behaves-differently-on-linux-compared-to-windows-regarding-escaped-whitespaces/6012/5 "2022-07-09T13:16:39Z")

</div>

> [@JohannesWilde](#):
>
> Could you hint me to somewhere, in order to understand why?

Other than the docs, quoting only works on whole “words”. It is not like bash (or other shells) where paired quotes can be jammed together to perform concatenation; in CMake the quotes become literal when they are not paired around words.
