_var=123 CurrentPath=`pwd`
for file in `ls /etc` do echo $file done
echo $_var _var2=$_var
echo $CurrentPathIs echo ${CurrentPath}Is
readonly
命令可以将变量定义为只读变量,只读变量的值不能再被改变。试图更改只读变量的值时会报错:bash: _var: readonly variable 。_var=123 readonly _var _var=456
unset
命令可以删除变量。如下,第二句echo没有任何输出。_var=123 echo $_var unset _var echo $_var
字符串可以用单引号、双引号标识,也可以不用引号。
your_name="runoob" # 使用双引号拼接 greeting="hello, "$your_name" !" greeting_1="hello, ${your_name} !" echo $greeting echo $greeting_1 # 使用单引号拼接 greeting_2='hello, '$your_name' !' greeting_3='hello, ${your_name} !' echo $greeting_2 echo $greeting_3
string="abcd" echo ${#string} #输出 4
string="runoob is a great site" echo ${string:1:4} # 输出 unoo
string="runoob is a great site" echo `expr index "$string" io` # 输出 4 echo `expr index "asdfvs" d` # 输出 3