Shell 脚本中的字符串连接错误 [英] Error in string Concatenation in Shell Scripting
问题描述
我是 Shell 脚本的初学者.
I am beginner to Shell scripting.
我使用了一个变量来存储值 A="MyScript"
.我尝试在后续步骤 $A_new
中连接字符串.令我惊讶的是它不起作用,$A.new
起作用了.
I have used a variable to store value A="MyScript"
. I tried to concatenate the string in subsequent steps $A_new
. To my surprise it didn't work and $A.new
worked.
你能帮我理解这些细节吗?
Could you please help me in understanding these details?
谢谢
推荐答案
变量名可以包含字母(a 到 z 或 A 到 Z)、数字(0 到 9)或下划线字符(_).
The name of a variable can contain letters ( a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).
Shell 不需要像 C
、C++
或 java
这样的编程语言中的任何变量声明.因此,当您编写 $A_new
shell 时,将 A_new
视为一个变量,您尚未为其分配任何值,因此它为空.
Shell does not require any variable declaration as in programming languages as C
, C++
or java
. So when you write $A_new
shell consider A_new
as a variable, which you have not assigned any value therefore it comes to be null.
要实现您提到的用途,请使用:${A}_new
To achieve what you mentioned use as :
${A}_new
在$
符号后将变量名括在大括号中以避免这种情况始终是一个好习惯.
Its always a good practice to enclose variable names in braces after $
sign to avoid such situation.
这篇关于Shell 脚本中的字符串连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!