当前位置:网站首页>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
边栏推荐
猜你喜欢
MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败
Shell编程的条件语句
Acwing 3208. Z字形扫描 偏移量+扩展图
注意力机制
今日睡眠质量记录71分
2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
VRRP + MSTP configuration, huawei eNSP experiment 】 【
TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)
【正点原子STM32连载】第三章 开发环境搭建 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
leetcode经典例题——56.合并区间
随机推荐
我和 TiDB 的故事 | 缘份在,那就终是能相遇的
加降息与BTC流动性事件策略研究
双指针方法
leetcode二叉树系列(二)
学习在php中分析switch与ifelse的执行效率
架构设计杂谈
陈春花发布声明,这场流量狂欢该到了收尾的时候
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
No module named 'flask_misaka' has been resolved [BUG solution]
字符串相关题目
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
什么是元宇宙?
ZbxTable 2.0 重磅发布!6大主要优化功能!
MindSpore:损失函数问题
IDEA启动热部署
有坦荡的远方
抬升市场投资情绪,若羽臣是否还需“自身硬”?
已解决No module named ‘flask_misaka‘【BUG解决】
Get the number of cpu cores
leetcode动态规划经典例题——53.最大子数组和