forked from shellever/ShellLearnings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.sh
More file actions
executable file
·32 lines (28 loc) · 1017 Bytes
/
array.sh
File metadata and controls
executable file
·32 lines (28 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
# 使用declare命令并指定-a选项来定义一个数组类型变量friends
declare -a friends
friends=(shirley pixie zach)
# 获取数组的所有元素
echo "\${friends[*]}=${friends[*]}"
echo "\${friends[@]}=${friends[@]}"
# 获取数组的第一个元素
echo "\${friends[0]}=${friends[0]}"
# 获取数组的第二个元素
echo "\${friends[1]}=${friends[1]}"
# 获取数组的第三个元素
echo "\${friends[2]}=${friends[2]}"
# 获取数组的长度,即数组元素的个数
echo "\${#friends[*]}=${#friends[*]}"
echo "\${#friends[@]}=${#friends[@]}"
# 获取数组第一个元素的长度
echo "\${#friends[0]}=${#friends[0]}"
# 获取数组第二个元素的长度
echo "\${#friends[1]}=${#friends[1]}"
# 获取数组第三个元素的长度
echo "\${#friends[2]}=${#friends[2]}"
# 切片方式获取内容
echo "\${friends[@]:1:2}=${friends[@]:1:2}"
# 从第二个元素开始
echo "\${friends[@]:2}=${friends[2]:2}"
# 到第二个元素为止
echo "\${friends[@]::2}=${friends[@]::2}"