In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Xiaobian to share with you in the Bash script to create and use the array method, I believe most people do not know how, so share this article for your reference, I hope you read this article after a great harvest, let us go to understand it!
An array is a data structure based on multiple elements of a key pair. Each array element is accessible by key index number. This article will cover creating arrays in bash scripts, initializing arrays, adding elements, updating elements, and deleting elements in bash scripts.
Define an array in Bash
There are two ways to create new arrays in bash scripts. The first is to use the declare command to define an Array. This command defines an associative array named test_array.
$ declare -a test_array
You can also create arrays by assigning elements.
$ test_array=(apple orange lemon)
Access array elements
Similar to other programming languages, bash array elements can be accessed using index numbers starting with 0 and then starting with 1, 2, 3,…n. This also applies to associative arrays with numeric index numbers.
$ echo ${test_array[0]}apple
Prints all elements of an array using @ or * instead of a specific index number.
$ echo $ {test_array [@]} apple orange lemon
Loop through arrays
You can also access array elements using loops in bash scripts. Loops are useful for traversing all array elements one by one and performing some operation on them.
for i in ${test_array[@]}do echo $idon
Add a new element to an array
You can add any number of elements to an existing array using the (+=) operation. Just add new elements, such as:
$ test_array+=(mango banana)
View array elements after adding new:
$ echo ${test_array[@]}apple orange lemon mango banana
Update array elements
To update an array element, simply assign any new value to an existing array by index. Let's change the current array element at index 2 with grapes.
$ test_array[2]=grapes
View array elements after adding new elements:
$ echo ${test_array[@]}apple orange grapes mango banana
Delete array elements
You can simply delete any array element using the index number. Here is how to remove the element at index 2 from the array in the bash script.
$ unset test_array [2]
View array elements after adding new elements:
$ echo ${test_array[@]}apple orange mango banana That's all about creating and using arrays in Bash scripts. Thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.