当前位置:网站首页>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
边栏推荐
- Detailed explanation of switch link aggregation [Huawei eNSP]
- leetcode经典例题——56.合并区间
- LeetCode中等题之旋转图像
- 2022年制冷与空调设备运行操作特种作业证考试题库及模拟考试
- RL学习笔记(一)
- 密码字典生成工具pydictor/john
- [Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1
- leetcode二叉树系列(一)
- MindSpore:model.train中的dataset_sink_mode该如何理解?
- MindSpore:MindSpore GPU版本安装问题
猜你喜欢
随机推荐
leetcode二叉树系列(一)
函数防抖与函数节流
并发编程之生产者和消费者问题
Win11怎么进行左右键对调?
【正点原子STM32连载】第四章 STM32初体验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
VRRP+MSTP配置详解【华为eNSP实验】
【正点原子STM32连载】第二章 STM32简介 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
LeetCode中等题之设计循环队列
冰蝎工具开发实现动态二进制加密WebShell
思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
ps抠图怎么抠出来,自学ps软件photoshop2022,ps怎么抠出想要的部分-笔记记录
sync-diff-inspector 使用实践
罗克韦尔AB PLC RSLogix5000中定时器指令使用方法介绍
MATLAB/Simulink快捷键
TiCDC同步延迟问题处理
cannot import name 'import_string' from 'werkzeug' [bug solution]
Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
注意力机制
OAK-FFC-4P全网首次测试









