# Forcing variable expansion

**URL:** https://discourse.cmake.org/t/forcing-variable-expansion/3769
**Category:** Code
**Created:** [July 16, 2021, 9:16am UTC](https://discourse.cmake.org/t/forcing-variable-expansion/3769 "2021-07-16T09:16:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Zomgrolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@Zomgrolf](https://discourse.cmake.org/u/Zomgrolf)
#### Post date: [July 16, 2021, 9:16am UTC](https://discourse.cmake.org/t/forcing-variable-expansion/3769/1 "2021-07-16T09:16:48Z")

</div>

Is there a way to force variable expansion on a string that contains a variable reference?

In my case I have a variable SOME\_VAR that is set by the user on the command line and contains a reference to a variable that is defined in the CMake script – something like this:

```auto
cmake ... -DSOME_VAR="${SOME_OTHER_VAR}_bar"

```

And my script defines SOME\_OTHER\_VAR to be “foo”. But if I then print out SOME\_VAR, I still see “${SOME\_OTHER\_VAR}\_bar”, rather than “foo\_bar”.

Is there something I can do to trigger variable expansion in SOME\_VAR?

---

<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 16, 2021, 9:59am UTC](https://discourse.cmake.org/t/forcing-variable-expansion/3769/2 "2021-07-16T09:59:22Z")

</div>

On the command line, that expansion is going to be controlled by the shell, not CMake. If you want replacement like that, you’ll need a CMake file to do it (maybe via the `-C` command line flag). But there’s no way to say “defer evaluation of this value until it is needed” in CMake.

---

<div class="post-metadata">

### Author: ![Zomgrolf](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@Zomgrolf](https://discourse.cmake.org/u/Zomgrolf)
#### Post date: [July 16, 2021, 10:21am UTC](https://discourse.cmake.org/t/forcing-variable-expansion/3769/3 "2021-07-16T10:21:21Z")

</div>

Not sure what my shell has to do with any of that? I neither expect nor want to perform any expansion “on the command line”.

Anyway, it seems that string(CONFIGURE) does exactly what I wanted.
