By the end of the post, you will learn everything about arrays and list such as definition, indexing, manipulating the arrays, and more.
Zsh Arrays and Lists
Arrays and lists are ordered collections of data that can store multiple values under a single variable name.
In Zsh, we can use the arrays to store and manipulate these collections efficiently. Arrays can hold various data types including strings, numbers, and even other arrays, making them versatile for various scripting tasks.
Array Declaration
We can declare an array in Zsh using the “typeset” or “declare” command. An example array declaration is as follows:
declare -a databases=("MySQL" "PostgreSQL" "SQLite" "Redis")
Accessing the Array Elements
To access the elements of an array, we can use the ${array[index]} notation where the “array” is the name of the array, and “index” is the position of the element.
NOTE: Array indexing in Zsh starts at index 1. Hence, the first element in the array has an index of 1.
This should return the first element from the specified array.
Array Indexing
As we mentioned, Zsh uses the 1-based indexing where the first element is at index 1, the second at 2, and so on.
You can also use the negative indices to access the elements from the end of the array as shown in the following example:
This should return the last element in the array.
Appending the Elements
Zsh allows us to add the elements to an existing array using the “+=” operator. An example is as follows:
This should append the provided value to the end of the array.
Removing the Elements
To remove the elements from an existing array, we can use the “unset” command with the element’s index.
This should remove the “SQLite” element from the array.
Array Slicing
We can extract a subset of an array, also known as a slice, by specifying the start and end indices. An example demonstration is as follows:
This should create a new array that contains the PostgreSQL and SQLite elements.
Array Iteration
We can loop through an array to perform an action on each element in the array. For example, we can use a “for” loop to iterate over each element in the array and print it to the terminal as follows:
echo "db: $db"
done
This should return each element from the databases array.
Multidimensional Arrays
Zsh also allows us to create and work with multidimensional arrays. This allows us to create the arrays of arrays. This can be useful for representing the tables or matrices.
An example is as follows:
To access an element in the matrix, we can follow the syntax as follows:
Arrays and Lists
In addition to arrays, Zsh has built-in support to work with lists. Lists are space-separated strings that we can convert to arrays and vice versa using the list and array types.
An example is as follows:
Use the following command to convert the list to an array:
array=("${(s: :)list_}")
We can also convert an array to a list using the following command:
This should convert an array into a list.
Conclusion
In this tutorial, you learned everything you need to know about working with arrays and lists when it comes to scripting with Zsh.