# execute\_command and arguments that contain with blank spaces

**URL:** https://discourse.cmake.org/t/execute-command-and-arguments-that-contain-with-blank-spaces/807
**Category:** Usage
**Created:** [March 16, 2020, 6:51pm UTC](https://discourse.cmake.org/t/execute-command-and-arguments-that-contain-with-blank-spaces/807 "2020-03-16T18:51:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sdettmer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sdettmer](https://discourse.cmake.org/u/sdettmer)
#### Post date: [March 16, 2020, 6:51pm UTC](https://discourse.cmake.org/t/execute-command-and-arguments-that-contain-with-blank-spaces/807/1 "2020-03-16T18:51:50Z")

</div>

Hi,

I tried two hours to pass an argument with a space to execute\_process, but I didn’t find out how to do it. Cmake performs some magic quoting (“VERBATIM”). I tried using variables, various escaping, but I could not get it done.

My use case is quite simple: I would like to get a path without the last dir:

`STRING(REGEX REPLACE /dir.*/ "x" PATH ${CMAKE_SOURCE_DIR}")`

this does not work since it is always “greedy” and “/tmp/dir/src/dir” returns “tmpx” instead “/tmp/dir/srcx”. So I tried using some

`execute_process(echo ${CMAKE_SOURCE_DIR} | bash -c 'stdin=$(cat); echo ${stdin%/dir/*}' OUTPUT_VARIABLE PATH)`

in various variations, with escaping every character by `\\`, using variables with the argument and more, but I didn’t get it right.

Could someone show me how to correctly write

`execute_process(bash -c 'echo a b c' OUTPUT_VARIABLE OUT)`

to get “abc” in ${OUT} please?

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [March 16, 2020, 10:46pm UTC](https://discourse.cmake.org/t/execute-command-and-arguments-that-contain-with-blank-spaces/807/2 "2020-03-16T22:46:44Z")

</div>

> [@sdettmer](#):
>
> I would like to get a path without the last dir:

Have you tried using [`get_filename_component`](https://cmake.org/cmake/help/latest/command/get_filename_component.html)?

Something like:

```cmake
get_filename_component(parent_dir "${CMAKE_CURRENT_SOURCE_DIR}" DIRECTORY)

```

might give the desired result.

> [@sdettmer](#):
>
> Could someone show me how to correctly write
> 
> `execute_process(bash -c 'echo a b c' OUTPUT_VARIABLE OUT)`
> 
> to get “abc” in ${OUT} please?

You would write it like this:

```cmake
execute_process(COMMAND "bash" "-c" "echo a b c" OUTPUT_VARIABLE OUT)

```

but you would get `a b c` in `OUT` (i.e. including the spaces between `a` and `b` and `c`).

See the [documentation of `execute_process`](https://cmake.org/cmake/help/latest/command/execute_process.html) for more details.

I hope this helps!

---

<div class="post-metadata">

### Author: ![sdettmer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sdettmer](https://discourse.cmake.org/u/sdettmer)
#### Post date: [March 17, 2020, 10:24am UTC](https://discourse.cmake.org/t/execute-command-and-arguments-that-contain-with-blank-spaces/807/3 "2020-03-17T10:24:33Z")

</div>

Hi!

Thanks so much for your helping and quick answer!

> [@McMartin](#):
>
> Have you tried using `get_filename_component`

Yes, but I forgot to mention that in my case it normally is not the last component that I want to be removed, but all up to one with a specific name, let’s say “rootdir”:  
`/tmp/prj1/rootdir/src/legacystuff/prjold/rootdir/src/something` → `/tmp/prj1/rootdir/src/legacystuff/prjold/`  
and the old bash script used `path="${path%/rootdir/*}"`.

> [@McMartin](#):
>
> You would write it like this:
> 
> ```auto
> execute_process(COMMAND "bash" "-c" "echo a b c" OUTPUT_VARIABLE OUT)
> 
> ```

Ohh thank you!

Indeed! It is so simple! I was sure this was exactly what I tried first! I must have been made another mistake.

Yes, this helped a lot!

So now I wrote

```auto
execute_process(
  COMMAND "bash" "-c" "path=${CMAKE_SOURCE_DIR}; echo \${path%/vobs/*}"
  OUTPUT_VARIABLE ROOTDIR
  OUTPUT_STRIP_TRAILING_WHITESPACE)

```

and got exactly the expected result, thank you!

In meantime I noticed that `string(FIND)` has a `REVERSE` that also can be used:

```auto
        string(FIND "${CMAKE_SOURCE_DIR}" "/vobs/" INDEX REVERSE)
        string(SUBSTRING "${CMAKE_SOURCE_DIR}" 0 ${INDEX} ROOTDIR)

```

which might even be better because avoids dependency to bash, but in my case there are many different bash commands used anyway.

Thanks again!
