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)

类变量ClassVariables(StaticFields)

局部变量LocalVariables

参数 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:

Thenameofavariablecancontainonlyletters(atozorAtoZ),numbers(0to9)ortheunderscorecharacter(_).

Byconvention,UnixShellvariableswouldhavetheirnamesinUPPERCASE.

Thefollowingexamplesarevalidvariablenames:

_ALI

TOKEN_A

VAR_1

VAR_2

Followingaretheexamplesofinvalidvariablenames:

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="helloworld"

[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~]#declareVAR

[root@jfht~]#echo"$VAR"

STRING

格式:declare VAR=

等同于VAR=

[root@jfht ~]# VAR=STRING

[root@jfht~]#declareVAR=

[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:notfound

[root@smsgwroot]#echo$?

1

[root@smsgwroot]#VAR=

[root@smsgwroot]#declare-pVAR

declare--VAR=""

[root@smsgwroot]#echo$?

0

[root@smsgwroot]#VAR=STRING

[root@smsgwroot]#declare-pVAR

declare--VAR="STRING"

[root@smsgw root]#

格式:${!VAR*}

这个参数扩展格式可以找出所有以VAR开头的变量来,但不能确切的知道是否有VAR这个变量,比如有可能还有VAR1。

man bash 写道
${!prefix*}

Expandstothenamesofvariableswhosenamesbeginwithprefix,

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

VARVAR1

[root@jfht ~]#

将变量声明为只读

格式:readonly VAR=STRING

help readonly 写道
readonly: readonly [-af] [name[=value] ...] or readonly -p

ThegivenNAMEsaremarkedreadonlyandthevaluesoftheseNAMEsmay

notbechangedbysubsequentassignment.Ifthe-foptionisgiven,

thenfunctionscorrespondingtotheNAMEsaresomarked.Ifno

argumentsaregiven,orif`-p'isgiven,alistofallreadonlynames

isprinted.The`-a'optionmeanstotreateachNAMEas

anarrayvariable.Anargumentof`--'disablesfurtheroption

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~]#unsetVAR

[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

>localSTR=Hello

>declareVAR=World

>}

[root@smsgwroot]#GGG=YesGlobal

[root@smsgwroot]#STR=NotHello

[root@smsgwroot]#VAR=NotWorld

[root@smsgwroot]#myfunc

[root@smsgwroot]#echo$GGG$STR$VAR

GlobalNotHelloNotWorld

[root@smsgw root]#

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

下节内容:Bash字符串处理(与Java对照) - 2.字符串的表示方式(字符串常量)