当前位置:网站首页>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
边栏推荐
- 【正点原子STM32连载】第二章 STM32简介 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
- MindSpore:【mindinsight】【Profiler】用execution_time推导出来的训练耗时远小于真实的耗时
- MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
- How to restore the Youxuan database with only data files
- MATLAB绘图总结
- 张朝阳对话俞敏洪:谈宇宙、谈焦虑、谈创业、谈退休、谈人生
- DOM简述
- ansible部署脚本--亲测可用无坑
- 学习使用php把stdClass Object转array的方法整理
- leetcode二叉树系列(二)
猜你喜欢
v-model原理,在“radio”、“checkbox”、“select”、修饰符
有坦荡的远方
双指针方法
菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿
蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路
冰蝎逆向初探
Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...
DOM简述
Detailed explanation of switch link aggregation [Huawei eNSP]
VRRP + MSTP configuration, huawei eNSP experiment 】 【
随机推荐
ansible部署脚本--亲测可用无坑
渗透——信息收集
各位大佬,请问mysql数据的cdc,能指定存量数据同步的zone为utc 吗
leetcode单调栈经典例题——最大矩形
leetcode经典例题——56.合并区间
加降息与BTC流动性事件策略研究
数据万象内容审核 — 共建安全互联网,专项开展“清朗”直播整治行动
leetcode二叉树系列(二叉搜索树篇)
字符串与正则表达式(C#)
Layer 3 Switch/Router OSPF Configuration Details [Huawei eNSP Experiment]
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
【C补充】指针相关知识点收集01
如何快速将Zabbix5.0升级至6.0?
【无标题】
Qt:小的任务管理器(task)
ZbxTable 2.0 重磅发布!6大主要优化功能!
Grafana9.0发布,Prometheus和Loki查询生成器、全新导航、热图面板等新功能!
Detailed explanation of telnet remote login aaa mode [Huawei eNSP]
leetcode动态规划经典例题——53.最大子数组和
Anton Paar安东帕密度计比重计维修DMA35性能参数