目录
一、高级变量赋值
1、高级变量赋值总结表
变量配置方式 | str 无配置 | str 为空字符串 | str 已配置为非空字符串 |
---|---|---|---|
var=${str-expr} | var=expr | var= | var=$str |
var=${str:-expr} | var=expr | var=expr | var=$str |
var=${str+expr} | var= | var=expr | var=expr |
var=${str:+expr} | var= | var= | var=expr |
var=${str=expr} | str=expr var=expr | str 不变 var= | str 不变 var=$str |
var=${str:=expr} | str=expr var=expr | str=expr var=expr | str 不变 var=$str |
var=${str?expr} | expr 输出至 stderr | var= | var=$str |
var=${str:?expr} | expr 输出至 stderr | expr 输出至 stderr | var=$str |
2、相关操作
(1)var=${str-hello} 变量配置方式
bash
#当str无配置时
[root@localhost ~]#unset str;var=${str-hello}
#当str为空字符串时
[root@localhost ~]#str=" ";var=${str-hello}
#当str为非空字符串时
[root@localhost ~]#str="1";var=${str-hello}

(2)var=${str:-hello} 变量配置方式
bash
#当str无配置时
[root@localhost ~]#unset str;var=${str:-hello}
#当str为空字符串时
[root@localhost ~]#str=" ";var=${str:-hello}
#当str为非空字符串时
[root@localhost ~]#str="1";var=${str:-hello}

(3)var=${str+hello} 变量配置方式
bash
#当str无配置时
[root@localhost ~]#unset str;var=${str+hello}
#当str为空字符串时
[root@localhost ~]#str=" ";var=${str+hello}
#当str为非空字符串时
[root@localhost ~]#str="1";var=${str+hello}

(4)var=${str:+hello} 变量配置方式
bash
#当str无配置时
[root@localhost ~]#unset str;var=${str:+hello}
#当str为空字符串时
[root@localhost ~]#str=" ";var=${str:+hello}
#当str为非空字符串时
[root@localhost ~]#str="1";var=${str:+hello}

(5)var=${str=hello} 变量配置方式
bash
#当str无配置时
[root@localhost ~]#unset str;var=${str=hello}
#当str为空字符串时
[root@localhost ~]#str=" ";var=${str=hello}
#当str为非空字符串时
[root@localhost ~]#str="1";var=${str=hello}

(6)var=${str:=hello} 变量配置方式
bash
#当str无配置时
[root@localhost ~]#unset str;var=${str:=hello}
#当str为空字符串时
[root@localhost ~]#str=" ";var=${str:=hello}
#当str为非空字符串时
[root@localhost ~]#str="1";var=${str:=hello}

(7)var=${str?hello} 变量配置方式
bash
#当str无配置时
[root@localhost ~]#unset str;var=${str?hello}
#当str为空字符串时
[root@localhost ~]#str=" ";var=${str?hello}
#当str为非空字符串时
[root@localhost ~]#str="1";var=${str?hello}

(8)var=${str:?hello} 变量配置方式
bash
#当str无配置时
[root@localhost ~]#unset str;var=${str:?hello}
#当str为空字符串时
[root@localhost ~]#str=" ";var=${str:?hello}
#当str为非空字符串时
[root@localhost ~]#str="1";var=${str:?hello}

二、变量间接引用
1、eval命令
eval
命令用于执行参数中包含的命令,并且可以替换其中的变量。当eval
命令被调用时,它会将参数作为一个命令来执行,并且在执行前会对参数进行两次扩展(expansion)。这意味着它会先进行变量扩展,然后进行命令扩展

