简体   繁体   中英

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.

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"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM