当前位置:网站首页>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
边栏推荐
猜你喜欢

下午14:00面试,14:08低着头出来了 ,问的实在是太...

抬升市场投资情绪,若羽臣是否还需“自身硬”?

Anton Paar Anton Paar Density Meter Hydrometer Repair DMA35 Performance Parameters

leetcode动态规划经典例题——53.最大子数组和

【C补充】指向指针或函数的指针

MindSpore:Ascend运行出现问题

路由/三层交换机DHCP下发地址详解【华为eNSP】
![Detailed explanation of switch link aggregation [Huawei eNSP]](/img/c2/f9797fe8b17a418466b60cc3dc50a1.png)
Detailed explanation of switch link aggregation [Huawei eNSP]

交换机链路聚合详解【华为eNSP】

Win11系统重装用什么好 一键重装Win11教程
随机推荐
leetcode经典例题——49.字母异位词分组
OAK-FFC-4P全网首次测试
VRRP+MSTP配置详解【华为eNSP实验】
学习使用php把stdClass Object转array的方法整理
After four years of outsourcing, the autumn recruits finally landed
软件工程国考总结——判断题
思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
JSP基本语法
用OpenGL绘制winXP版扫雷的笑脸表情
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
【云驻共创】HCSD 大咖直播–就业指南
请你谈谈网站是如何进行访问的?【web领域面试题】
Qt:小的任务管理器(task)
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
Win11文件资源管理器找不到选项卡怎么办?
命里有时终须有--记与TiDB的一次次擦肩而过
暴力破解-破解 Apache BASIC 认证
v-model原理,在“radio”、“checkbox”、“select”、修饰符
redis解决分布式session问题
数据万象内容审核 — 共建安全互联网,专项开展“清朗”直播整治行动