5
shell:脚本数组list的操作整理
source link: https://blog.51cto.com/mouday/5867861
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
shell:脚本数组list的操作整理
精选 原创# 定义数组,等号两边不要有空格
list=(
"A"
"B"
)
# 数组长度
echo ${#list[@]} # 2
# 获取数组元素
echo ${list[1]} # B
# 输出所有值
echo ${list[@]} # A B
# 循环打印数组
for var in ${list[@]}; do
echo "var = ${var}"
done
# 输出:
# var = A
# var = B
字符串转数组
# 字符串转数组
str="A B C"
arr=($str)
# 数组长度
echo ${#arr[@]} # 3
# 所有数组元素
echo ${arr[@]}
# A B C
# 字符串转数组
str="A,B,C"
arr=(`echo $str | tr ',' ' '`)
# 数组长度
echo ${#arr[@]} # 3
# 所有数组元素
echo ${arr[@]}
# A B C
- 打赏
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK