2025-02-10

Bash字符串處理(與Java對照) – 1.(字符串)變量聲明

In Java
Java中變量名稱的規則
Java變量名稱是區分大小寫的。變量名稱是一個合法的標識符,沒有長度限制,由Unicode字符、數字、美元符、下劃線組成,不能是關鍵字和保留字。最好是字母開頭,跟上字母或數字,采用Camel命名規則。
The Java Tutorials (http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) 寫道
Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".The convention, however, is to always begin your variable names with a letter, not "$" or "_".
 
Java變量的類型
實例變量 Instance Variables (Non-Static Fields)
類變量 Class Variables (Static Fields)
局部變量 Local Variables
參數 Parameters
 
在Java中定義字符串變量
String s1;
String s2 = "1243";
String s3 = s1 + s2;
 
static final String BASH = "BASH";
 
In Bash
變量名稱
Shell變量名稱,隻能包括字母、數字和下劃線,並且隻能以字母和下劃線開頭。通常情況下,Shell變量名稱為大寫的形式。(PS:找遍瞭 info bash、man bash、Advanced Bash-Scripting Guide等都沒有找到關於Shell變量名稱的規則說明,最後隻能求助於互聯網瞭。)
Unix – Using Shell Variables (http://www.tutorialspoint.com/unix/unix-using-variables.htm) 寫道
Variable Names:
The name of a variable can contain only letters ( a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).
By convention, Unix Shell variables would have their names in UPPERCASE.

The following examples are valid variable names:
_ALI
TOKEN_A
VAR_1
VAR_2

Following are the examples of invalid variable names:
2_VAR
-VARIABLE
VAR1-VAR2
VAR_A!

The reason you cannot use other characters such as !,*, or – is that these characters have a special meaning for the shell.
 
聲明變量並賦值
在Bash中,變量並不區分類型,因此也不需要給變量指定類型。
在Bash中聲明變量不需要顯式進行,給變量賦值即可
格式:VAR=STRING
格式:VAR="STRING"
格式:VAR='STRING'
VAR為變量名
STRING為字符串
要註意的是,等號(=)兩側不能有空白。
如果字符串中間有空白或特殊字符,則需要用單引號或雙引號引起來(參見“Bash字符串處理(與Java對照) – 2.字符串的表示方式(字符串常量)”)。
 
[root@jfht ~]# STR=hello
[root@jfht ~]# STR=hello\ world
[root@jfht ~]# STR="hello world"
[root@jfht ~]# STR='hello world'
 
引用變量(訪問變量)
如果要得到VAR變量的值,就需要引用變量,有如下兩種格式:
格式:$VAR
格式:${VAR}
 
Unix – Using Shell Variables (http://www.tutorialspoint.com/unix/unix-using-variables.htm) 寫道
Accessing Values:
To access the value stored in a variable, prefix its name with the dollar sign ( $):
 
要註意的是,在給變量賦值時,變量名不能加上$。
格式:VAR=STRING    而不是 $VAR=STRING
 
使用declare命令來聲明變量
在Bash中有一個declare指令,也可以定義變量,效果與VAR=STRING相同
格式:declare VAR=STRING
 
[root@jfht ~]# declare STR=dummy
[root@jfht ~]# echo $STR
dummy
 
聲明變量但不設置初始值
格式:VAR=
等號右側不寫任何字符,即設置變量VAR為空。
 
[root@jfht ~]# VAR=
[root@jfht ~]# echo "$VAR"

[root@jfht ~]#
 
 
格式:declare VAR
使用declare聲明變量,如果變量原來聲明過,則其值不變,即沒有復位變量的功能。
 
[root@jfht ~]# VAR=STRING
[root@jfht ~]# echo "$VAR"
STRING
[root@jfht ~]# declare VAR
[root@jfht ~]# echo "$VAR"
STRING
 
 
格式:declare VAR=
等同於VAR=
[root@jfht ~]# VAR=STRING
[root@jfht ~]# declare VAR=
[root@jfht ~]# echo "$VAR"

[root@jfht ~]#
 
判斷變量是否聲明?
格式:declare -p VAR
顯示VAR變量是否聲明過瞭,如果沒有打印 not found,否則顯示它的屬性和值。
help declare 寫道
The -p option
will display the attributes and values of each NAME.
 
[root@smsgw root]# declare -p VAR
-bash: declare: VAR: not found
[root@smsgw root]# echo $?
1
[root@smsgw root]# VAR=
[root@smsgw root]# declare -p VAR
declare — VAR=""
[root@smsgw root]# echo $?
0
[root@smsgw root]# VAR=STRING
[root@smsgw root]# declare -p VAR
declare — VAR="STRING"
[root@smsgw root]#
 
格式:${!VAR*}
這個參數擴展格式可以找出所有以VAR開頭的變量來,但不能確切的知道是否有VAR這個變量,比如有可能還有VAR1。
man bash 寫道
${!prefix*}
Expands to the names of variables whose names begin with prefix,
separated by the first character of the IFS special variable.
 
[root@jfht ~]# echo ${!VAR}

[root@jfht ~]# VAR=
[root@jfht ~]# echo ${!VAR}

[root@jfht ~]# echo ${!VAR*}
VAR
[root@jfht ~]# VAR1=
[root@jfht ~]# echo ${!VAR*}
VAR VAR1
[root@jfht ~]#
 
將變量聲明為隻讀
格式:readonly VAR=STRING
help readonly 寫道
readonly: readonly [-af] [name[=value] …] or readonly -p
The given NAMEs are marked readonly and the values of these NAMEs may
not be changed by subsequent assignment. If the -f option is given,
then functions corresponding to the NAMEs are so marked. If no
arguments are given, or if `-p' is given, a list of all readonly names
is printed. The `-a' option means to treat each NAME as
an array variable. An argument of `–' disables further option
processing.
 
格式:declare -r VAR=STRING
help declare 寫道
-r to make NAMEs readonly
 
取消變量、刪除變量
格式:unset VAR
 
[root@jfht ~]# VAR=STRING
[root@jfht ~]# echo "$VAR"
STRING
[root@jfht ~]# unset VAR
[root@jfht ~]# echo "$VAR"

[root@jfht ~]#
 
定義局部變量
在默認情況下,變量都是全局變量。在前面加上local或者declare之後就變成瞭局部變量。
格式:local VAR
格式:local VAR=STRING
格式:declare VAR
格式:declare VAR=STRING
局部變量在函數中使用,如下所示
myfunc() {
    GGG=Global
    # GGG變量是全局變量
    local STR=Hello
    # STR變量的有效范圍隻在myfunc函數內
    declare VAR=World
    # VAR變量也是局部變量
}
 
 
[root@smsgw root]# myfunc(){
> GGG=Global
> local STR=Hello
> declare VAR=World
> }
[root@smsgw root]# GGG=YesGlobal
[root@smsgw root]# STR=NotHello
[root@smsgw root]# VAR=NotWorld
[root@smsgw root]# myfunc
[root@smsgw root]# echo $GGG $STR $VAR
Global NotHello NotWorld
[root@smsgw root]#

作者“Bash @ Linux”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *