Melodie Gauthier
Melodie Gauthier

Reputation: 715

ksh difference between $ and ${}

Can someone explain to me what is the difference, in Korn shell, between:

ANOTHER_VAR=${SOME_VAR}

and

ANOTHER_VAR=$SOME_VAR

I came across these types of declaration and can't see what the difference is.

Upvotes: 4

Views: 2518

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754520

One has two brace characters around the name and the other doesn't; otherwise, in this context, there is no difference.

However, if you had:

ONE_VAR="$TWO_VAR_$THREE_VAR"
UNO_VAR="${TWO_VAR}_${THREE_VAR}"

then the values in $ONE_VAR and $UNO_VAR will be different unless both $TWO_VAR and $TWO_VAR_ exist and $TWO_VAR_ holds the value that is stored in $TWO_VAR plus a trailing underscore (where $TWO_VAR could be an empty string, or undefined, as long as $TWO_VAR_ holds just an underscore).

Thanks to William Pursell for pointing out a minor inaccuracy in the previous version.

There are many contexts where you must use the braces, such as:

UNE_VAR=${YET_ANOTHER_VAR:-"default setting"}

Upvotes: 6

Related Questions