What Is the Syntax to Print All the Elements of an Array in Unix Shell Scripting?


Angela Bailey

Unix shell scripting provides a powerful set of tools for automating tasks and manipulating data. One common operation is printing all the elements of an array. In this tutorial, we will explore the syntax to achieve this in Unix shell scripting.

Declaring an Array

Before we can print the elements of an array, we need to declare and initialize it. In Unix shell scripting, arrays are created using the declare keyword followed by the name of the array and its elements enclosed in parentheses.

declare -a myArray=("element1" "element2" "element3")

Printing All Elements

To print all the elements of an array, we can use a looping construct. The most commonly used loop in Unix shell scripting is the for loop. Let’s see how we can utilize it to achieve our goal:

# Declare and initialize an array
declare -a myArray=("element1" "element2" "element3")

# Loop through each element and print
for element in "${myArray[@]}"
    echo $element

Analyzing the Code

  • The first line declares and initializes our array with three elements.
  • In the for loop, we iterate over each element of the array using ${myArray[@]}.
  • The current element is stored in the variable $element.
  • We then use the echo command to print each element on a new line.

You can modify the code as per your requirements. For example, you can add conditional statements within the loop to perform certain operations based on the values of the array elements.


In Unix shell scripting, printing all elements of an array is a straightforward process using the for loop. By understanding this syntax, you can manipulate and display array data efficiently in your shell scripts.

Now that you have learned how to print all the elements of an array in Unix shell scripting, you can apply this knowledge to various scenarios where array manipulation is required. Happy scripting!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy