当前位置:网站首页>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
边栏推荐
- redis解决分布式session问题
- 四大网络攻击常见手段及防护
- 学习在微信小程序中判断url的文件后缀格式
- IDEA 自动导入的配置(Auto import)
- I am 37 this year, and I was rushed by a big factory to...
- ISO14443A读卡流程(作为示例参考)
- Cloud function to achieve automatic website check-in configuration details [Web function/Nodejs/cookie]
- 密码字典生成工具pydictor/john
- Post-94 Byte P7 posted the salary slip: It's really good to make up for this...
- telnet远程登录aaa模式详解【华为eNSP】
猜你喜欢

冰蝎工具开发实现动态二进制加密WebShell

命里有时终须有--记与TiDB的一次次擦肩而过

蘑菇书EasyRL学习笔记

Win10电脑经常发出叮咚声音怎么关闭

三层交换机/路由器OSPF配置详解【华为eNSP实验】
![No module named 'flask_misaka' has been resolved [BUG solution]](/img/cc/e379d23a41330d2335dd192e16e821.png)
No module named 'flask_misaka' has been resolved [BUG solution]

【正点原子STM32连载】第二章 STM32简介 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1

ISO14443A读卡流程(作为示例参考)
![Detailed explanation of telnet remote login aaa mode [Huawei eNSP]](/img/cf/aaf3a0b794b1076423fc5b90ecc9f0.png)
Detailed explanation of telnet remote login aaa mode [Huawei eNSP]

云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
随机推荐
加降息与BTC流动性事件策略研究
MindSpore:【mindinsight】【Profiler】用execution_time推导出来的训练耗时远小于真实的耗时
学习使用php把stdClass Object转array的方法整理
TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)
Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]
【COS 加码福利】COS 用户实践有奖征文,等你来投稿!
MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败
并发编程之生产者和消费者问题
MindSpore:损失函数问题
基于cRIO-904X搭建Simulink与Labview环境
Anton Paar安东帕密度计比重计维修DMA35性能参数
请问同一个oracle cdc表,如果flink job重新提交,是会全量读取一遍源数据还是增量呢?
学习在微信小程序中判断url的文件后缀格式
Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]
下午14:00面试,14:08低着头出来了 ,问的实在是太...
【C补充】指向指针或函数的指针
他97年的,我既然卷不过他...
架构设计杂谈
VRRP+MSTP配置详解【华为eNSP实验】
2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告