当前位置:网站首页>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:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
![Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]](/img/82/f76e87689599f3501a13f83c0fa970.png)
Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]

已解决No module named ‘flask_misaka‘【BUG解决】

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

redis解决分布式session问题

TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2

Win7C盘不能扩大怎么办?Win7C盘扩展卷不能点怎么解决

被Win11安全中心误删除的文件怎么恢复?

Win11系统重装用什么好 一键重装Win11教程

【COS 加码福利】COS 用户实践有奖征文,等你来投稿!
随机推荐
cannot import name ‘import_string‘ from ‘werkzeug‘【bug解决】
继承和static关键字
蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路
ansible部署脚本--亲测可用无坑
各位大佬,请问mysql数据的cdc,能指定存量数据同步的zone为utc 吗
Shell编程的条件语句
leetcode经典例题——56.合并区间
leetcode动态规划经典例题——53.最大子数组和
【c】操作符详解(二)
leetcode单调栈经典例题——最大矩形
VRRP + MSTP configuration, huawei eNSP experiment 】 【
有坦荡的远方
Anton Paar安东帕密度计比重计维修DMA35性能参数
MySQL binlog都有哪些模式?
leetcode经典例题——49.字母异位词分组
Libtomcrypt AES 加密及解密
交换机链路聚合详解【华为eNSP】
户外徒步旅行
开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
蘑菇书EasyRL学习笔记