How to print array elements on a separate line in Bash
In this tutorial, we will learn how to print the array elements on a separate line in Bash.
Consider, we have the following prices array in Bash:
prices=(10 20 30)
Printing the array elements
To print the array elements on a separate line, we can use the printf
command with the %s
format specifier and newline character \n
in Bash.
Here is an example:
prices=(10 20 30)
printf '%s\n' "${prices[@]}"
Output:
10
20
30
@$
expands the each element in the array as a separate argument.
%s
is a format specifier for a string that adds a placeholder to the array element.
\n
adds the line break after each element in an array.