当前位置:网站首页>shell 数组
shell 数组
2022-06-25 13:32:00 【响彻天堂丶】
1 介绍
Shell 支持数组(Array), 数组是若干数据的集合,其中的每一份数据都称为数组的元素。只支持一维数组,不支持多维数组。
2 语法
在 Shell 中,用括号( )来表示数组,数组元素之间用空格来分隔. 语法为:
array_name=(item1 item2 ...) # 方式1
array_name=([索引下标1]=item1 [索引下标2]=item2 ...) # 方式2
3 数组的获取
1 通过下标获取元素值,index从0开始
${arr[index]}
2 .获取值同时复制给其他变量
item=${arr[index]}
3 使用 @ 或 * 可以获取数组中的所有元素
${arr[@]}
${arr[*]}
4 获取数组的长度或个数
${#arr[@]}
${#arr[*]}
5 获取数组指定元素的字符长度
${#arr[索引]}
demo:
#!/bin/bash
array=(1 2 3 4 5 6 "hello world" 38)
echo "array所有元素为: ${array[*]}"
echo "array长度为: ${#array[*]}"
echo "index下标为0的值: ${array[0]}"
echo "index下标为6的值: ${array[6]}"
echo "index下标为6的字符串长度为:${#array[6]}"

4 数组的拼接
4.1 介绍
所谓 Shell 数组拼接(数组合并),就是将两个数组连接成一个数组
4.2 语法
使用 @ 和 * 获取数组所有元素之后进行拼接
array_new=(${array1[@]} ${array2[@]} ...)
array_new=(${array1[*]} ${array2[*]} ...)
4.3 演示
#!/bin/bash
array1=(1 2 3)
array2=(4 5 6)
array3=(${
array1[*]} ${
array2[*]})
echo "array3 数组长度: ${#array3[*]}"
echo "array3 全部元素: ${array3[*]}"

5 数组的删除
5.1 介绍
删除数组指定元素数据和删除整个数组数据
5.2 语法
#删除数组指定元素数据
unset array_name[index]
#删除整个数组
unset array_name
5.3 demo
#!/bin/bash
array1=(1 2 3)
array2=(4 5 6)
echo "array1: ${array1[*]}"
echo "删除数组array1"
unset array1
echo "array1: ${array1[*]}"
echo "array2: ${array2[*]}"
echo "删除array1下标为0的元素"
unset array2[0]
echo "删除后 array1: ${array2[*]}"

边栏推荐
- Is it safe to open an account with tongdaxin stock?
- 请问通达信股票开户是安全的吗?
- Knowledge of initial C language 2.0
- Judge whether it is a mobile terminal
- Openstack learning notes (I)
- K-line diagram 24 classic diagrams (shadow)
- 腾讯云搭建Socks5多IP代理服务器实现游戏单窗口单IP完美搭建教程附带工具「建议收藏」
- Insight into heap and stack stored in new string() /string() in JS
- Settings the PC must be turned on
- 多臺雲服務器的 Kubernetes 集群搭建
猜你喜欢

Simple realization of mine sweeping

As a software testing engineer, how do you think to ensure software quality?

Getting started with numpy Library

Windows下MySQL的安装和删除

用NumPy实现神经网络(Mysteries of Neural Networks Part III)

网络远程访问的方式使用树莓派

Explication d'un problème de manuel

Implementation of a small book system

Some knowledge of the initial C language
![Leetcode: Sword finger offer II 091 Painting house [2D DP]](/img/d7/dc8a3522dbd58b4573cfd75497460c.png)
Leetcode: Sword finger offer II 091 Painting house [2D DP]
随机推荐
数据库中显示error1822,error1824
Test your earning power? What will you do in the future?
OpenStack学习笔记(二)
论文阅读:Graph Contrastive Learning with Augmentations
“移动云杯”算力网络应用创新大赛火热报名中!
As a software testing engineer, how do you think to ensure software quality?
Deeply understand the mathematics behind deep neural networks (mysteries of neural networks Part I)
使用调试工具调试博图TCP连接所遇到的问题
Websocket -- reverse proxy to solve cross domain problems
Hash table, hash conflict
Leetcode: Sword finger offer II 091 Painting house [2D DP]
Insight into heap and stack stored in new string() /string() in JS
Cesium learning notes
API in Nova
【开源鸿蒙系统展示】RK3568开发板搭载OpenHarmony 3.1 Release
k线图24种经典图解(影线篇)
Graph contractual learning with augmentations
Kubernetes cluster construction of multiple ECS
Discuz仿今日头条模板/Discuz新闻资讯商业版GBK模板
leetcode:剑指 Offer II 091. 粉刷房子【二维dp】