基本语法
数组定义
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:~$
来源:CSDN
作者:偷一个月亮
链接:https://blog.csdn.net/yiqiushi4748/article/details/103811427