变量的截取与替换
这个网站的总结有错误之处,予以修改:
格式 |
说明 |
${string: start :length} |
从 string 字符串的左边第 start 个(从0开始计数)字符开始,向右截取 length 个字符。 |
${string: start} |
从 string 字符串的左边第 start 个字符开始截取,直到最后。 |
${string: 0-start :length} |
从 string 字符串的右边第 start 个字符开始,向右截取 length 个字符。 |
${string: 0-start} |
从 string 字符串的右边第 start 个字符开始截取,直到最后。 |
${string#*chars} |
从 string 字符串第一次出现 chars 的位置开始,截取 chars 右边的所有字符。 |
${string##*chars} |
从 string 字符串最后一次出现 chars 的位置开始,截取 chars 右边的所有字符。 |
${string%chars*} |
从 string 字符串最后一次出现 chars 的位置开始,截取 chars 左边的所有字符。 |
${string%%chars*} |
从 string 字符串第一次出现 chars 的位置开始,截取 chars 左边的所有字符。 |
1. 截取
符号 |
意义 |
# |
从左边非贪婪删除匹配的字符串 |
## |
从左边贪婪删除匹配的字符串 |
%、%% |
从右边同上 |
[shark@linux ~]$ str="123456.89"
[shark@linux ~]$ echo ${str:2}
3456.89
[shark@linux ~]$ echo ${str:0:2}
12
[shark@linux ~]$ echo ${str:2:2}
34
[shark@linux ~]$ echo ${str:0-9}
123456.89
[shark@linux ~]$ echo ${str:0-9:5}
12345
[shark@linux ~]$ str=123123.321321
[shark@linux ~]$ echo ${str:0:5}
12312
[shark@linux ~]$ echo ${str:0-0:5}
12312
[sink@dev ~]$ echo ${MAIL}
/var/spool/mail/sink
[sink@dev ~]$ echo ${MAIL#*/}
var/spool/mail/sink
[sink@dev ~]$ echo ${MAIL##*/}
sink
[sink@dev ~]$ echo ${MAIL#/var}
/spool/mail/sink
# 可见必须是从第一个字符就开始匹配
[sink@dev ~]$ echo ${MAIL#/mail}
/var/spool/mail/sink
[sink@dev ~]$ echo ${MAIL#/var*/}
spool/mail/sink
[sink@dev ~]$ echo ${MAIL##/var*/}
sink
2. 替换
符号 |
意义 |
/ |
替换第一个匹配的字符串 |
// |
替换所有匹配的字符串 |
[sink@dev ~]$ echo ${PATH/sbin/SBIN/}
/home/clion-2018.3/bin:/usr/local/bin:/usr/bin:/usr/local/SBIN/:/usr/sbin:/home/sink/.local/bin:/home/sink/bin
[sink@dev ~]$ echo ${PATH//sbin/SBIN/}
/home/clion-2018.3/bin:/usr/local/bin:/usr/bin:/usr/local/SBIN/:/usr/SBIN/:/home/sink/.local/bin:/home/sink/bin
3. 测试与替换
字符串替换格式:
new_var=${old_var符号new_value}
符号 |
意义 |
不加“:” |
测试该值未设置 |
加“:” |
测试该值未设置或为空字符串(空值) |
|
|
- |
变量 未设置,赋值 |
+ |
若变量 未设置,不进行赋值 |
= |
若变量 未设置 则全部赋新值,且old_var也会被赋值;否则 全部赋旧值 |
? |
若变量 未设置 则将new_value作为错误信息输出到 标准错误输出;否则 全部赋旧值 |
# -,若变量 未设置 则赋值
# 未设置,赋值
[sink@dev ~]$ unset old_var; unset new_var
[sink@dev ~]$ new_var=${old_var-new_value}; declare -p new_var old_var
declare -- new_var="new_value"
-bash: declare: old_var: not found
# 空值,等同于有值的情况,不赋值
[sink@dev ~]$ unset old_var; unset new_var; old_var=""
[sink@dev ~]$ new_var=${old_var-new_value}; declare -p new_var old_var
declare -- new_var=""
declare -- old_var=""
# 有值,等同于空值的情况,不赋值
[sink@dev ~]$ unset old_var; unset new_var; old_var="old_value"
[sink@dev ~]$ new_var=${old_var-new_value}; declare -p new_var old_var
declare -- new_var="old_value"
declare -- old_var="old_value"
# :-,若变量 未设置或为空值 则赋值
# +,若变量 未设置,不进行赋值
# 未设置,不赋值
[sink@dev ~]$ unset old_var; unset new_var
[sink@dev ~]$ new_var=${old_var+new_value}
[sink@dev ~]$ declare -p new_var old_var
declare -- new_var=""
-bash: declare: old_var: not found
# 空值,等同于有值的情况,赋值
[sink@dev ~]$ unset old_var; unset new_var; old_var=""
[sink@dev ~]$ new_var=${old_var+new_value}; declare -p new_var old_var
declare -- new_var="new_value"
declare -- old_var=""
# 有值,等同于空值的情况,赋值
[sink@dev ~]$ unset old_var; unset new_var; old_var="old_value"
[sink@dev ~]$ new_var=${old_var+new_value}; declare -p new_var old_var
declare -- new_var="new_value"
declare -- old_var="old_value"
# :+,若变量 未设置或为空值则不 进行赋值
# 未设置,等同于空值的情况,不赋值
[sink@dev ~]$ unset old_var; unset new_var
[sink@dev ~]$ new_var=${old_var:+new_value}; declare -p new_var old_var
declare -- new_var=""
-bash: declare: old_var: not found
# 空值,等同于未设置的情况,不赋值
[sink@dev ~]$ unset old_var; unset new_var; old_var=""
[sink@dev ~]$ new_var=${old_var:+new_value}; declare -p new_var old_var
declare -- new_var=""
declare -- old_var=""
# 有值,赋值
[sink@dev ~]$ unset old_var; unset new_var; old_var="old_value"
[sink@dev ~]$ new_var=${old_var:+new_value}; declare -p new_var old_var
declare -- new_var="new_value"
declare -- old_var="old_value"
# =,若变量 未设置 则全部赋新值;否则 全部赋旧值
# 未设置,全部赋值
[sink@dev ~]$ unset old_var; unset new_var
[sink@dev ~]$ new_var=${old_var=new_value}; declare -p new_var old_var
declare -- new_var="new_value"
declare -- old_var="new_value"
# 空值=有值,不赋值
[sink@dev ~]$ unset old_var; unset new_var; old_var=""
[sink@dev ~]$ new_var=${old_var=new_value}; declare -p new_var old_var
declare -- new_var=""
declare -- old_var=""
# 有值=空值,不赋值
[sink@dev ~]$ unset old_var; unset new_var; old_var="old_value"
[sink@dev ~]$ new_var=${old_var=new_value}; declare -p new_var old_var
declare -- new_var="old_value"
declare -- old_var="old_value"
# :=,若变量 未设置或为空值 则全部赋新值;否则 全部赋旧值
# 未设置
[sink@dev ~]$ unset old_var; unset new_var
[sink@dev ~]$ new_var=${old_var:=new_value}; declare -p new_var old_var
declare -- new_var="new_value"
declare -- old_var="new_value"
# 空值
[sink@dev ~]$ unset old_var; unset new_var; old_var=""
[sink@dev ~]$ new_var=${old_var:=new_value}; declare -p new_var old_var
declare -- new_var="new_value"
declare -- old_var="new_value"
# 有值
[sink@dev ~]$ unset old_var; unset new_var; old_var="old_value"
[sink@dev ~]$ new_var=${old_var:=new_value}; declare -p new_var old_var
declare -- new_var="old_value"
declare -- old_var="old_value"
# ?,若变量 未设置 则将值输出到 标准错误输出;否则 全部赋旧值
# 未设置,输出错误信息
[sink@dev ~]$ unset old_var; unset new_var
[sink@dev ~]$ new_var=${old_var?error_info}
-bash: old_var: error_info
[sink@dev ~]$ echo $?
1
[sink@dev ~]$ declare -p new_var old_var
-bash: declare: new_var: not found
-bash: declare: old_var: not found
# 空值=有值,全部赋旧值
[sink@dev ~]$ unset old_var; unset new_var; old_var=""
[sink@dev ~]$ new_var=${old_var?error_info}; declare -p new_var old_var
declare -- new_var=""
declare -- old_var=""
# 已设置,全部赋旧值
[sink@dev ~]$ unset old_var; unset new_var; old_var="old_value"
[sink@dev ~]$ new_var=${old_var?error_info}; declare -p new_var old_var
declare -- new_var="old_value"
declare -- old_var="old_value"
# :?,若变量 未设置或为空值 则将值输出到 标准错误输出;否则 全部赋旧值