Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 为什么bash字符串连接失败?_String_Bash_Concatenation - Fatal编程技术网

String 为什么bash字符串连接失败?

String 为什么bash字符串连接失败?,string,bash,concatenation,String,Bash,Concatenation,我认为它应该可以工作的原因是,我将其视为sprinf或任何类似C的字符串格式化程序(%s_%s或%sx%s)问题在于\uu是变量名中的有效字符。要区分这一点,请使用大括号: #!/bin/sh VAR_A=100 VAR_B=200 FOO="$VAR_A_$VAR_B"; # 200 BAZ="$VAR_Ax$VAR_B"; # 200 echo $FOO; # 200 instead of 100_200 echo $BAZ; # 200 instead of 100x200 为了保

我认为它应该可以工作的原因是,我将其视为sprinf或任何类似C的字符串格式化程序(%s_%s或%sx%s)

问题在于
\uu
是变量名中的有效字符。要区分这一点,请使用大括号:

#!/bin/sh

VAR_A=100
VAR_B=200

FOO="$VAR_A_$VAR_B"; # 200
BAZ="$VAR_Ax$VAR_B"; # 200

echo $FOO; # 200 instead of 100_200
echo $BAZ; # 200 instead of 100x200
为了保持一致性,可以在任何地方使用大括号:

FOO="${VAR_A}_$VAR_B"

bash
无法确定变量名是什么。尝试
${VAR\u A}{VAR\u B}
。使用变量时也要养成引用变量的习惯:
echo“$FOO”
FOO="${VAR_A}_${VAR_B}"