# Yet another command line 'spaces in arguments' problem...is this really 2022?

**URL:** https://discourse.cmake.org/t/yet-another-command-line-spaces-in-arguments-problem-is-this-really-2022/5829
**Category:** Usage
**Created:** [June 9, 2022, 1:32am UTC](https://discourse.cmake.org/t/yet-another-command-line-spaces-in-arguments-problem-is-this-really-2022/5829 "2022-06-09T01:32:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![marksibly](https://discourse.cmake.org/user_avatar/discourse.cmake.org/marksibly/32/4303_2.png) [@marksibly](https://discourse.cmake.org/u/marksibly)
#### Post date: [June 9, 2022, 1:32am UTC](https://discourse.cmake.org/t/yet-another-command-line-spaces-in-arguments-problem-is-this-really-2022/5829/1 "2022-06-09T01:32:24Z")

</div>

Hi,

This has been driving me nuts all morning!

Why does this work OK?

```auto
export CHECKOUT_DIR=openxr-checkout
export BUILD_DIR=openxr-build
cmake -S "$CHECKOUT_DIR" -B "$BUILD_DIR" -DDYNAMIC_LOADER=OFF -G "Visual Studio 17 2022"

```

But this doesn’t?

```auto
export BUILD_CONFIG="-DDYNAMIC_LOADER=OFF -G \"Visual Studio 17 2022\""
cmake -S "$CHECKOUT_DIR" -B "$BUILD_DIR" $BUILD_CONFIG
--->
CMake Error: Could not create named generator "Visual
...
CMake Warning:
  Ignoring extra path from command line:

   "2022""

```

It’s like something’s ignoring the quotes. If I run the first ‘plain’ version without quotes I get a similar error, only also without quotes, ie: ‘Could not create named generator Visual’.

If echo $BUILD\_CONFIG I get the expected (IMO) result, ie: quotes have been properly escaped:

-DDYNAMIC\_LOADER=OFF -G “Visual Studio 17 2022”

I am of course on Windows and using the version of bash that comes with git, which may have something to do with it.

This may well be nothing to do with cmake, but it’s an oddity I’ve never encountered before.

Any ideas?

cmake version 3.23.2

Bye!  
Mark

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [June 9, 2022, 6:32am UTC](https://discourse.cmake.org/t/yet-another-command-line-spaces-in-arguments-problem-is-this-really-2022/5829/2 "2022-06-09T06:32:17Z")

</div>

It has nothing to do with CMake and everything to do with the shell…  
I was frustrated by the same thing some time ago and did a deeper research that lead to one conclusion: JUST DON’T.  
Unfortunately normal rules regarding argument passing do not apply when using variables on the command line. It’s next to impossible to get quoted arguments working this way.

---

<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: [June 9, 2022, 3:21pm UTC](https://discourse.cmake.org/t/yet-another-command-line-spaces-in-arguments-problem-is-this-really-2022/5829/3 "2022-06-09T15:21:04Z")

</div>

Depending on the shell in use (looks like a bash?), the escape markers are different. I would recommend using `set -x` to have the shell output what it sees before execution (if it is indeed a bash-alike).

---

<div class="post-metadata">

### Author: ![marksibly](https://discourse.cmake.org/user_avatar/discourse.cmake.org/marksibly/32/4303_2.png) [@marksibly](https://discourse.cmake.org/u/marksibly)
#### Post date: [June 9, 2022, 10:53pm UTC](https://discourse.cmake.org/t/yet-another-command-line-spaces-in-arguments-problem-is-this-really-2022/5829/4 "2022-06-09T22:53:40Z")

</div>

OK, I’ve had a play around with this and it’s definitely a limitation of shell(s) IMO.

I’ve tried it now in Manjaro bash, fish and Windows git bash, and in all cases shell can’t tell the difference between an ‘argument separating’ space and a ‘real space in arg/filename’ when interpolating vars. Simplest example I could come up with:

```auto
mkdir "test dir"
export TEST="-l \"test dir\""
ls $TEST

```

No matter how I try to escape the space in ‘test dir’, shell always splits $TEST into 3 args to ls. I confirmed this with a little printf argv app.

Moral of the story is probably to never put multiple command args in a var, unless you know for sure that none of them contain space chars?

Bye,  
Mark

---

<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: [June 9, 2022, 11:46pm UTC](https://discourse.cmake.org/t/yet-another-command-line-spaces-in-arguments-problem-is-this-really-2022/5829/5 "2022-06-09T23:46:46Z")

</div>

You can use the `bash` array extension:

```bash
args=("arg1" "arg 2")
ls "${args[@]}"

```

---

<div class="post-metadata">

### Author: ![marksibly](https://discourse.cmake.org/user_avatar/discourse.cmake.org/marksibly/32/4303_2.png) [@marksibly](https://discourse.cmake.org/u/marksibly)
#### Post date: [June 10, 2022, 1:07am UTC](https://discourse.cmake.org/t/yet-another-command-line-spaces-in-arguments-problem-is-this-really-2022/5829/6 "2022-06-10T01:07:41Z")

</div>

Cool, that does seem to work thanks.
