Bash字符串处理(与Java对照) - 1.(字符串)变量声明(一)

2014-11-24 02:40:30 · 作者: · 浏览: 0

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 pre