当前位置:网站首页>bash shell数组详解
bash shell数组详解
2022-08-04 09:33:00 【CheerTan】
原始文章:https://codefather.tech/blog/bash-array/
Bash Indexed Array of Strings
We will start by creating an indexed array of strings where the strings are directory names in a Linux system:
dirs=(“/etc” “/var” “/opt” “/tmp”)
First of all let’s see what gets printed when we echo the value of the array variable dirs:
$ echo $dirs
/etc
When you print a Bash array variable the result is the first element of the array.
Another way to print the first element of the array is by accessing the array based on its index.
Bash indexed arrays are zero based, this means that to access the first element we have to use the index zero.
$ echo ${dirs[0]}
/etc
Wonder why we are using curly brackets?
We can understand why by removing them to see what the ouput is:
$ echo $dirs[0]
/etc[0]
Bash prints the first element of the array followed by [0] because it only recognises $dirs as a variable. To include [0] as part of the variable name we have to use curly brackets.
In the same way, to print the second element of the array we will access the index 1 of the array:
$ echo ${dirs[1]}
/var
What if we want to access the last element of the array?
Before doing that we have to find out how to get the length of a Bash array…
How Do You Determine the Length of a Bash Array?
To find the length of an array in Bash we have to use the syntax ${#array_name[@]}.
Let’s apply it to our example:
$ echo ${#dirs[@]}
4
The syntax might seem hard to remember when you see it for the first time…
…but don’t worry, just practice it a few times and you will remember it.
Access the Last Element of a Bash Array
Now that we know how to get the number of elements in a Bash array, we can use this information to retrieve the value of the last element.
First we have to calculate the index of the last element that is equal to the number of elements in the array minus one (remember that Bash arrays are zero-based as usually happens in most programming languages).
${#dirs[@]}-1
This value will be the index to pass when we want to print the last element of the array:
$ echo KaTeX parse error: Expected '}', got 'EOF' at end of input: {dirs[{#dirs[@]}-1]}
/tmp
Definitely not one of the easiest ways to retrieve the last element of an array, if you are familiar with other programming languages
Since Bash 4.2 arrays also accept negative indexes that allow to access elements starting from the end of the array.
To verify your version of Bash use the following command:
$ bash --version
边栏推荐
猜你喜欢
2022年制冷与空调设备运行操作特种作业证考试题库及模拟考试
【正点原子STM32连载】第三章 开发环境搭建 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
Win11系统重装用什么好 一键重装Win11教程
Libtomcrypt AES 加密及解密
【正点原子STM32连载】第一章 本书学习方法 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
并发编程之生产者和消费者问题
RL学习笔记(一)
【云驻共创】HCSD 大咖直播–就业指南
MindSpore:Ascend运行出现问题
cannot import name 'import_string' from 'werkzeug' [bug solution]
随机推荐
三层交换机配置MSTP协议详解【华为eNSP实验】
陈春花发布声明,这场流量狂欢该到了收尾的时候
Anton Paar Anton Paar Density Meter Hydrometer Repair DMA35 Performance Parameters
leetcode动态规划经典例题——53.最大子数组和
TiCDC迁移-TiDB到MySQL测试
2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
No module named 'flask_misaka' has been resolved [BUG solution]
Acwing 3208. Z字形扫描 偏移量+扩展图
Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]
Win11怎么进行左右键对调?
ZbxTable 2.0 重磅发布!6大主要优化功能!
四大网络攻击常见手段及防护
我和 TiDB 的故事 | TiDB 对我不离不弃,我亦如此
开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
MindSpore:Batchnorm only support nchw input!
ps抠图怎么抠出来,自学ps软件photoshop2022,ps怎么抠出想要的部分-笔记记录
用OpenGL绘制winXP版扫雷的笑脸表情
LeetCode中等题之旋转图像
grafana手册之可视化配置图表table
How Oracle for current library or certain library data on the same server number?