# When to use ${} around variable names

**URL:** https://discourse.cmake.org/t/when-to-use-around-variable-names/1021
**Category:** Code
**Created:** [April 14, 2020, 7:12pm UTC](https://discourse.cmake.org/t/when-to-use-around-variable-names/1021 "2020-04-14T19:12:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Robert\_Boehne](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert_boehne/32/523_2.png) [@Robert\_Boehne](https://discourse.cmake.org/u/Robert_Boehne)
#### Post date: [April 14, 2020, 7:12pm UTC](https://discourse.cmake.org/t/when-to-use-around-variable-names/1021/1 "2020-04-14T19:12:15Z")

</div>

All,

Recently I put up a change in our product’s CMakeLists.txt to eliminate unnecessary use of ${} in tests around variables CMAKE\_SYSTEM\_NAME and CMAKE\_CXX\_COMPILER\_ID. That prompted a colleague to ask “when is it necessary to use ${} around variable names?” and I had to admit my ignorance 😉

Is there a rule or guidelines for when the ${} is necessary?

TIA,

Rob Boehne

---

<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: [April 14, 2020, 7:38pm UTC](https://discourse.cmake.org/t/when-to-use-around-variable-names/1021/2 "2020-04-14T19:38:22Z")

</div>

The only place they aren’t necessary (and it’s not being passed as an output variable to a command) is in the `if` and `while` commands and the `foreach (IN LISTS)` right-hand side. Basically, if you have a binary predicate, you can take either argument in the form of `"${var}"` and just use `var`. Note that unary predicates like `DEFINED`, `TARGET`, `IS_ABSOLUTE`, etc. do _not_ do automatic dereferencing.

---

<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: [April 14, 2020, 9:30pm UTC](https://discourse.cmake.org/t/when-to-use-around-variable-names/1021/3 "2020-04-14T21:30:59Z")

</div>

There’s also things like the `list()` command where it expects the name of a variable to operate on rather than the variable’s contents.

---

<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: [April 14, 2020, 10:50pm UTC](https://discourse.cmake.org/t/when-to-use-around-variable-names/1021/4 "2020-04-14T22:50:27Z")

</div>

Ah, I put that under the “output variable” classification. It’s just _also_ an input 🙂 .
