$ VARIABLE和$ {VARIABLE}之间有什么区别

爱国者*_*爱国者 7 linux variables bash

任何人都可以请我解释为什么有些Linux专家建议我们在Bash脚本中使用$ {VARIABLE}?似乎没有任何差别.

cni*_*tar 10

假设您想$VARIABLE立即打印"string"

echo "$VARIABLEstring"    # tries to print the variable called VARIABLEstring
echo "${VARIABLE}string"  # prints $VARIABLE and then "string"
Run Code Online (Sandbox Code Playgroud)

Bash还支持使用此语法进行字符串操作.


Dav*_* Hu 5

您可能想要这样做的原因之一是{}充当分隔符:

a=42
echo "${a}sdf"  # 42sdf
echo "$asdf"  # prints nothing because there's no variable $asdf
Run Code Online (Sandbox Code Playgroud)