Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash中${}的用法 #2

Open
jinwandalaohu opened this issue Apr 25, 2022 · 0 comments
Open

bash中${}的用法 #2

jinwandalaohu opened this issue Apr 25, 2022 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@jinwandalaohu
Copy link
Owner

jinwandalaohu commented Apr 25, 2022

在 bash 中 ${} 用于设置变量默认值和字符串取值切片以及变量的间接引用,详细用法如下图,在 Centos6 下字符串取子 ${STR:POSITON:LENGTH},LENGTH 为负数会报错。

1、${VAR},取出变量 VAR 值

[root@localhost ~]# a=hjks[root@localhost ~]# echo ${a}```

2、${VAR:-DEFAULT},VAR没有定义或者为空则输出$DEFAULT的值(VAR不变)

```bash
[root@localhost ~]# unset a [root@localhost ~]# echo ${a:-hello};echo $a```

3、${VAR:=DEFAULT},VAR没有定义或空则为$DEFAULT的值

```bash
[root@localhost ~]# echo ${a:=hello};echo a```

4、${VAR+VALUE}${VAR+VALUE},VAR定义则输出$VALUE,否则为空字符串(VAR不变)

```bash
[root@localhost ~]# a=hello[root@localhost ~]# echo ${a:+yemo};echo $a[root@localhost ~]# unset a[root@localhost ~]# echo ${a:+yemo};echo $a```

5、${VAR?MSG},VAR未定义则打印$MSG

```null
[root@localhost ~]# unset a[root@localhost ~]# echo ${a?this is null}[root@localhost ~]# typeset -i a[root@localhost ~]# echo ${a?this is null}```

6、${VAR:?MSG},VAR未定义或者为空则打印$MSG

```null
[root@localhost ~]# unset a[root@localhost ~]# echo ${a?this is null}[root@localhost ~]# typeset -i a[root@localhost ~]# echo ${a?this is null}```

7、${!PERFIX\*}  ${!PERFIX@}匹配所有以PERFIX开头的变量

```null
[root@localhost ~]# echo ${!RAND*}[root@localhost ~]# echo ${!RAND@}```

8、${!VAR}变量间接引用,也可以用eval echo \\$$VAR

```bash
[root@localhost ~]# b=12345[root@localhost ~]# echo ${!a}[root@localhost ~]# eval echo \$$a```

9、${#STR},返回STR长度

```null
[root@localhost ~]# b=12345[root@localhost ~]# echo ${#b}```

10、${STR^^},把STR中的所有小写字母转换为大写输出(不改变值)

```null
[root@localhost ~]# unset a b[root@localhost ~]# a="hello Rie Kugimiya"   #钉宫理惠老师,声音很萌很可爱[root@localhost ~]# echo ${a^^}[root@localhost ~]# echo $a```

11、${STR,,},把STR中的所有大写字母转换为小写输出(不改变值)

```null
[root@localhost ~]# echo ${a,,}[root@localhost ~]# echo $a```

12、${STR:POSITON},从$POSITON位置提取子串

```null
[root@localhost ~]# echo $a[root@localhost ~]# echo ${a:5}[root@localhost ~]# echo ${a: -5}   #反向从末尾开始计数$POSITON位置的子串```

13、${STR:POSITON:LENGTH},从$POSITON位置提取长度为$LENGTH子串

```null
[root@localhost ~]# echo $a[root@localhost ~]# echo ${a:5:6}                #从第5个开始向后取6个字符[root@localhost ~]# echo ${a:5:-3}          #从第五个开始去去掉倒数3个字符 [root@localhost ~]# echo ${a: -5:-3}        #取倒数5个字符去掉倒数3个字符im                                    #由于减号是前面的关键字所以POSITON前面得加空格   [root@localhost ~]# a="daisuki Rie Kugimiya"    #依旧是可爱的钉宫理惠老师[root@localhost ~]# echo ${a:5:6}[root@localhost ~]# echo ${a:5:-4}-bash: -4: substring expression < 0[root@localhost ~]# echo ${a: -5:-2}-bash: -2: substring expression < 0[root@localhost ~]# echo ${a: -5:2}            #从-5位置开始去2个字符长度的子串```

14、${STR#SUBSTR},从$STR头查找匹配,删除最短匹配$SUBSTR的子串

${arry#SUBSTR},对于数组,对每个数组元素单独匹配,执行删除子串操作

```null
[root@localhost ~]# echo ${a}[root@localhost ~]# echo ${a#*R}ie Kugimiya                            #从左向右匹配*R匹配到daisuki R,删除并返回剩下的[root@localhost ~]# b=(daisuki Rie Kugimiya)[root@localhost ~]# echo ${b[@]}[root@localhost ~]# echo ${b[@]#*i}   #删除了b[0]的dai,b[1]的Ri,b[2]的 Kugi```

15、${STR##SUBSTR},从$STR头部查找匹配,删除最长匹配$SUBSTR的子串

```null
[root@localhost ~]# echo $a                    #前面很详细了,后面就不解释了[root@localhost ~]# echo ${a##*i}[root@localhost ~]# echo ${b[@]##*i}```

16、${STR%SUBSTR},从$STR尾部查找匹配,删除最短匹配$SUBSTR的子串

```null
[root@localhost ~]# echo ${a%i*}[root@localhost ~]# echo ${b[@]%i*}```

17、${STR%%SUBSTR},从$STR尾部查找匹配,删除最长匹配$SUBSTR的子串

```null
[root@localhost ~]# echo ${a%%i*}[root@localhost ~]# echo ${b[@]%%i*}```

18、${STR/SUBSTR/REPLACE},使用$REPALCE替换$STR第一个匹配的$SUBSTR

```null
[root@localhost ~]# echo ${a/i/%%/}[root@localhost ~]# echo ${b[@]/i/%%/}da%%/suki R%%/e Kug%%/miya```

19、${STR//SUBSTR/REPLACE},使用$REPALCE替换$STR中所有匹配的$SUBSTR

```null
[root@localhost ~]# echo ${a//i/%%/}da%%/suk%%/ R%%/e Kug%%/m%%/ya[root@localhost ~]# echo ${b[@]//i/%%/}da%%/suk%%/ R%%/e Kug%%/m%%/ya```

20、${STR/#SUBSTR/REPLACE}$STR$STR开头的,则用$REPLACE来替换匹配到的$SUBSTR

```null
[root@localhost ~]# echo ${a/#su/%%}[root@localhost ~]# echo ${a/#dai/%%}[root@localhost ~]# echo ${b[@]/#K/@}[root@localhost ~]# echo ${b[@]/#K/$$}[root@localhost ~]# echo ${b[@]/#K/$#}[root@localhost ~]# echo ${b[@]/#K/666}```

21、${STR/%SUBSTR/REPLACE}$STR$STR结尾的,则用$REPLACE来替换匹配到的$SUBSTR

```null
[root@localhost ~]# echo ${a/%dai/##}[root@localhost ~]# echo ${a/%miya/##}[root@localhost ~]# echo ${b[@]/%ie/666}[root@localhost ~]# echo ${b[@]/%ie/$$}```

Bash函数或者脚本接受连续参数到数组

```null
 for i in `seq 0 $(($#-1))`;do        arry[$((i-1))]=`eval echo \$$i````

**参考文献:** 

Linux系统命令和Shell脚本实践指南 
 [https://blog.csdn.net/qq_25518029/article/details/119564512](https://blog.csdn.net/qq_25518029/article/details/119564512)
@jinwandalaohu jinwandalaohu added the documentation Improvements or additions to documentation label Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant