zsh

Zsh Scripting Lesson: Using Arrays and Lists Effectively in Your Scripts

In this tutorial, we will learn everything about using arrays and lists. These are some of the comprehensive and effective sequence of data types provided by Zsh and they play an essential role when working with collections.

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:

typeset -a databases=("MySQL" "PostgreSQL" "SQLite" "Redis")

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.

echo "db: ${databases[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:

echo "value: ${databases[-1]}"

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:

databases+=("Elasticsearch")

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.

unset 'databases[3]'

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:

sliced_databases=("${databases[2,4]}")

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:

for db in "${databases[@]}"; do

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:

typeset -a matrix=("1 2 3" "4 5 6" "7 8 9")

To access an element in the matrix, we can follow the syntax as follows:

echo " row 2, column 3: ${matrix[2][3]}"

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:

list_ ="1 2 3 4 5"

Use the following command to convert the list to an array:

typeset -a array
array=("${(s: :)list_}")

We can also convert an array to a list using the following command:

list_a ="${(j: :)array}"

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list