基本语法(数组)--Linux

半腔热情 提交于 2020-01-25 08:42:02

基本语法

数组定义

majun@instance-zqtg07w6:~$ pa
pa: command not found
majun@instance-zqtg07w6:~$ echo $?
127
majun@instance-zqtg07w6:~$ declare -a array
majun@instance-zqtg07w6:~$ echo $array

majun@instance-zqtg07w6:~$ array[0]=111
majun@instance-zqtg07w6:~$ array[1]=333
majun@instance-zqtg07w6:~$ array[2]="helloworld"


数组操作

数组取值

majun@instance-zqtg07w6:~$ echo $array
111
majun@instance-zqtg07w6:~$ echo $array[2]
111[2]
majun@instance-zqtg07w6:~$ echo ${array[2]}
helloworld
majun@instance-zqtg07w6:~$ echo ${array[1]}
333

majun@instance-zqtg07w6:~$ echo ${array[@]}
111 333 helloworld
majun@instance-zqtg07w6:~$ echo ${array[*]}
111 333 helloworld

majun@instance-zqtg07w6:~$ echo ${#array[2]}
10
majun@instance-zqtg07w6:~$ echo ${array[*]:1:2}
333 helloworld
majun@instance-zqtg07w6:~$ echo ${array[2]:3:6}
loworl
majun@instance-zqtg07w6:~$
majun@instance-zqtg07w6:~$ nmae=('join','kali')
majun@instance-zqtg07w6:~$ name=('join','kali')
majun@instance-zqtg07w6:~$ echo ${name[*]}
join,kali
majun@instance-zqtg07w6:~$ name=('join' 'kali')
majun@instance-zqtg07w6:~$ echo ${name[*]}
join kali
majun@instance-zqtg07w6:~$ echo ${name[0]}
join
majun@instance-zqtg07w6:~$ coon=(${array[*]} ${name[*]})
majun@instance-zqtg07w6:~$ echo ${coon[?]}
bash: ?: syntax error: operand expected (error token is "?")
majun@instance-zqtg07w6:~$ echo ${coon[@]}
111 333 helloworld join kali
majun@instance-zqtg07w6:~$



majun@instance-zqtg07w6:~$ echo ${coon[@]}
111 333 helloworld
majun@instance-zqtg07w6:~$ coon=(${coon[@]/333/tom})
majun@instance-zqtg07w6:~$ echo ${coon[@]}
111 tom helloworld
majun@instance-zqtg07w6:~$ unset coon[0]
majun@instance-zqtg07w6:~$ echo ${coon[@]}
tom helloworld
majun@instance-zqtg07w6:~$


所有的练习脚本都在:
https://github.com/SaltNego/Learn_linux_bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!