diff --git a/sources/tech/20230710.1 ⭐️ Bash Basics Series 5 Using Arrays in Bash.md b/sources/tech/20230710.1 ⭐️ Bash Basics Series 5 Using Arrays in Bash.md new file mode 100644 index 0000000000..279902eeff --- /dev/null +++ b/sources/tech/20230710.1 ⭐️ Bash Basics Series 5 Using Arrays in Bash.md @@ -0,0 +1,169 @@ +[#]: subject: "Bash Basics Series #5: Using Arrays in Bash" +[#]: via: "https://itsfoss.com/bash-arrays/" +[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/" +[#]: collector: "lkxed" +[#]: translator: " " +[#]: reviewer: " " +[#]: publisher: " " +[#]: url: " " + +Bash Basics Series #5: Using Arrays in Bash +====== + +In the earlier part of the series, you learned about variables. The variables can have a single value in it. + +Arrays can have several values inside it. This makes things easier when you have to deal with several variables at a time. You don't have to store individual values in a new variable. + +So, instead of declaring five variables like this: + +``` +distro1=Ubuntu +distro2=Fedora +distro3=SUSE +distro4=Arch Linux +distro5=Nix +``` + +You can initialize all of them in a single array: + +``` +distros=(Ubuntu Fedora SUSE "Arch Linux" Nix) +``` + +Unlike some other programming languages, you don't use commas as array element separators. + +That's good. Let's see how to access the array elements. + +### Accessing array elements in bash + +The array elements are accessed using the index (position in the array). To access array element at index N, use: + +``` +${array_name[N]} +``` + +> 💡 Like most other programming languages, the array starts at index 0 in Bash shell. This means the first element has index 0, the second element has index 1 and the `nth` element has index `n-1`. + +So, if you want to print the SUSE, you'll use: + +``` +echo ${distros[2]} +``` + +![Example of accessing array elements in bash shell][1] + +> 🚧 There must not be any white space after `${` or before `}`. You CANNOT use it like `${ array[n] }`. + +### Access all array elements at once + +Let's say you want to print all the elements of an array. + +You may use echo `${array[n]}` one by one but that's really not necessary. There is a better and easier way: + +``` +${array[*]} +``` + +That will give you all the array elements. + +![Accessing all array elements at once in bash shell][2] + +### Get array length in bash + +How do you know how many elements are there in an array? There is a dedicated way to [get array length in Bash][3]: + +``` +${#array_name[@]} +``` + +That's so simple, right? + +![Get array length in bash][4] + +### Add array elements in bash + +If you have to add additional elements to an array, use the `+=` operator to [append element to existing array in bash][5]: + +``` +array_name+=("new_value") +``` + +Here's an example: + +![Append new element to array][6] + +> 🚧 It is important to use `()` while appending an element. + +You can also use the index to set the element at any position. + +``` +array_name[N]=new_value +``` + +**But remember to use the correct index number.** If you use it on an existing index, the new value will replace the element. + +If you use an 'out of bound' index, it will still be added after the last element. For example, if the array length is six and you try to set a new value at index 9, it will still be added as the last element at the 7th position (index 6). + +![][7] + +### Delete an array element + +You can use `unset` shell built-in to remove an array element by providing the index number: + +``` +unset array_name[N] +``` + +Here's an example, where I delete the 4th element of the array. + +![Delete array element in bash][8] + +You can also delete the entire array with unset: + +``` +unset array_name +``` + +> 💡 There are no strict data type rules in Bash. You can create an array that contains integers and strings both. + +### 🏋️ Exercise time + +Let's practice what you learned about bash arrays. + +**Exercise 1**: Create a bash script that has an array of five best Linux distros. Print them all. + +Now, replace the middle choice with Hannah Montanna Linux. + +**Exercise 2**: Create a bash script that accepts three numbers from the user and then prints them in reverse order. + +Expected output: + +``` +Enter three numbers and press enter +12 23 44 +Numbers in reverse order are: 44 23 12 +``` + +I hope you are enjoying learning bash shell scripting with this series. In the next chapter, you'll learn about using if-else. Stay tuned. + +-------------------------------------------------------------------------------- + +via: https://itsfoss.com/bash-arrays/ + +作者:[Abhishek Prakash][a] +选题:[lkxed][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://itsfoss.com/author/abhishek/ +[b]: https://github.com/lkxed/ +[1]: https://itsfoss.com/content/images/2023/07/accessing-array-elements-bash.png +[2]: https://itsfoss.com/content/images/2023/07/accessing-all-array-elements-bash.png +[3]: https://linuxhandbook.com:443/array-length-bash/ +[4]: https://itsfoss.com/content/images/2023/07/get-array-length-bash.png +[5]: https://linuxhandbook.com:443/bash-append-array/ +[6]: https://itsfoss.com/content/images/2023/07/append-element-to-array.png +[7]: https://itsfoss.com/content/images/2023/07/add-array-element-bash-1.png +[8]: https://itsfoss.com/content/images/2023/07/delete-array-element-bash.png