局部变量
环境变量
Shell变量
# 变量的声明name = "zhangsan"# 变量的调用echo $nameecho ${name}# 只读变量 /bin/sh: NAME: This variable is read only.url = "https://www.google.com"readonly urlurl = "https://www.runoob.com"# 删除变量unset name
双引号:
# 声明字符串str1 = "hello world 1"str2 = 'hello world 2'str3 ='hello world3# 字符串拼接 -- 双引号name = 'sunwukong'name1 = "hello, " $name "" //可以拼接name2 = "hello, ${name} " //可以拼接# 字符串拼接 -- 单引号passwd = '123456'passwd1 = 'hello, ' $passwd '' //可以拼接passwd2 = 'hello, ${passwd}' //不能拼接echo $passwd2 # hello, ${passwd} !# 字符串的长度email = "123456@qq.com"echo ${#email} //获取字符串的长度echo ${email:1:4} //获取email字符串从1开始, 取4个字符(字符串的计数是从开始的)
# 定义数组 括号来表示数组,数组元素用 " 空格 " 符号分割开数组名 = ( 值 1 值 2 ... 值 n)favs = ( " 足球 " " 蓝球 " " 乒乓球球 " " 保龄球 " )# 读取数组 ${ 数组名 [ 下标 ]}fav = ${favs[1]}# 使用 @ 符号可以获取数组中的所有元素echo ${favs[@]}# 获取数组的长度length1 = ${#favs[@]}length2 = ${#favs[*]}
#--------------------------------------------# 这是一个注释# author :# site :#--------------------------------------------##### 服务器配置 -start ############### 服务器配置 -end ###### 特殊的多行注释:<<EOF注释内容 ...注释内容 ...注释内容 ...EOF:<<!注释内容 ...注释内容 ...注释内容 ...!注: 这里EOF和!都是可以更改的, 但是得保证前面和后面都得是一样的
执行Shell脚本时, 向脚本传递参数, 脚本内获取参数的格式为: $n, n代表一个数字.
#!/bin/bashecho "Shell 传递参数实例! " ;echo " 执行的文件名: $0 " ;echo " 第一个参数为: $1 " ;echo " 第二个参数为: $2 " ;echo " 第三个参数为: $3 " ;# ./hello.sh 11 22 33 44