当前位置:网站首页>shell使用数组
shell使用数组
2022-06-21 14:46:00 【用户6978604】
定义数组
使用括号定义数组,定义的时候可以用元素进行初始化,也可以直接定义:
array1=()
array2=(item1 item2 item3)访问数组:
一般格式是:
${arrayName[index]}例如:
echo ${array2[1]}数组的下标跟C语言一样是从0开始的,所以上面输出的是item2
也可以访问全部元素,下面两种方法是等价的:
echo ${array2[*]}
echo ${array2[@]}追加元素
array2[50]="abc"
array2[30]="def"然后让我们看一下现在的数组是怎么样子的:
#!/bin/bash
array1=()
array2=(item1 item2 item3)
array2[50]="abc"
array2[30]="def"
echo ${array2[*]}
------------------------------
[email protected]:/usr/study$ ./array.sh
item1 item2 item3 def abc可以发现,追加元素时候下标不需要是连续的,当我们输出全部元素的时候,元素是空值的会自动省略。
获取长度:
len=${#array2[*]}或者
len=${#array2[@]}也可以获取数组元素的长度:
indexLen=${#array2[50]}这里的indexLen指的是元素的字符个数。
边栏推荐
- Select article (040) - what is the log output when you click this paragraph?
- How to write a rotation chart
- Tcp/ip Basics
- Kubeneters' CNI network plug-in installation Kube ovn
- !!** The test is valid * *:vscode "the terminal will be reused by the task, press any key to close" /vscode the terminal runs NPM to pop up the select program dialog box / try a new cross platform Pow
- 阶乘求和
- Word thesis typesetting tutorial
- kernel GDB
- Understand the use of protobuf serialization protocol
- C language selection type supplement
猜你喜欢

Qt-7-multithreading

Why is epoll efficient?

Taobao secsha plug-in

Operator Tour (I)

Use Matplotlib to draw the first figure

PCA dimension reduction application of OpenCV (I)

2022 Hunan latest fire facility operator simulation test question bank and answers

So many statistical charts? This visualizer is great~~

Explain the design idea and capacity expansion mechanism of ThreadLocal in detail

Analysis of ROC and AUC
随机推荐
USB message capture tcpdump
JS written test question: asynchronous
C language selection type supplement
Calculate subtotal of advertising business
Pyqt5 learning notes of orange_ Connect to SQL Server database
Explain the high availability features of Nacos
Clickhouse client connection
Nodejs setting domestic source
Dplayer development barrage background
Reasonably set the number of threads 【 rpm 】
Use ant for running program with command line arguments
Work assessment of contract law of Jilin University in March of the 22nd spring -00100
C语言的指针
Compile time annotation automatically generates dataholder
Redisson distributed lock design from a bug
Interview - difference between pointer and reference
Subshell
Read distributed consistency protocols 2pc and 3pc
English accumulation__ annoyance
Decipher Bishengyuan: is it tea that consumers buy, or is it IQ tax?