Bash

Print array elements on separate lines in Bash

25 September 2026 · 4 min read

Print array elements on separate lines in Bash

Working with arrays in Bash often involves the need to display their contents clearly. Whether you’re debugging a script, processing data, or simply presenting information, printing array elements on separate lines is a fundamental skill. This post provides a comprehensive guide to various techniques for achieving this, catering to different scenarios and complexities. We’ll explore built-in commands, loops, and even delve into formatting options for enhanced readability.

Using the printf Command

The printf command offers a concise and efficient way to print array elements on individual lines. Its format specifier %s\n dictates that each element should be treated as a string (%s) followed by a newline character (\n). This ensures each element appears on a new line.

For example: printf "%s\n" "${my_array[@]}". This simple command iterates through the entire array my_array and prints each element followed by a newline. The [@] notation is crucial as it expands to all elements of the array.

This method is particularly useful for simple arrays where you need a quick and clean output. It’s also highly portable across different Bash versions.

Looping Through the Array

For more complex scenarios, looping through the array provides greater control. Using a for loop allows you to process each element individually.

bash for element in “${my_array[@]}”; do echo “$element” done

This approach is highly flexible. Within the loop, you can perform additional operations on each element before printing it. For instance, you could add prefixes, suffixes, or even filter elements based on specific criteria.

Looping is ideal when you need to manipulate or format the output beyond simply printing each element on a new line.

Leveraging the IFS Variable

The Internal Field Separator (IFS) variable controls how Bash interprets word boundaries. By temporarily modifying IFS to a newline character, you can print the array elements separated by newlines.

bash IFS=$’\n’; echo “${my_array[]}”; unset IFS

This method is concise but requires careful handling of IFS. It’s important to reset IFS to its default value after use to avoid unexpected behavior in subsequent commands. The [] notation joins the array elements with the first character of IFS.

Advanced Formatting and Output Control

For sophisticated formatting, combining loops with other commands like awk or sed provides extensive control. You can add numbering, prefixes, or even color-code the output.

Consider a scenario where you want to number each element: bash i=1 for element in “${my_array[@]}”; do printf “%d. %s\n” “$i” “$element” i=$((i+1)) done This adds a numerical prefix to each element, enhancing readability, especially for larger arrays.

This flexibility is crucial when presenting data in a structured and user-friendly manner.

Choosing the Right Method

The best method depends on the specific needs of your script. For simple output, printf is efficient. For complex processing and formatting, looping provides greater control. Using IFS is concise but requires careful handling.

  • Simplicity: printf
  • Flexibility: Looping
  • Conciseness: IFS (with caution)

Understanding these techniques empowers you to handle arrays efficiently and display their contents effectively in various Bash scripting scenarios.

FAQ

Q: What if my array contains elements with spaces?

A: Using "${my_array[@]}" with double quotes preserves whitespace within array elements, preventing them from being split into separate words.

Choosing the right technique for printing array elements on separate lines depends on the complexity of your task. Simple arrays often benefit from the conciseness of printf, while more intricate operations require the flexibility of loops. By mastering these methods, you can effectively manage and display array data within your Bash scripts. Remember to consider the potential impact of IFS manipulation and utilize double quotes to preserve whitespace within array elements. Learn more about advanced Bash scripting techniques. Dive deeper into Bash array manipulation and unlock the full potential of your scripts.

  1. Assess your array structure and output requirements.
  2. Choose the most appropriate technique (printf, looping, or IFS).
  3. Implement the chosen method, paying attention to whitespace preservation and IFS handling.
  4. Test thoroughly to ensure the desired output.

[Infographic Placeholder]

Question & Answer :
How do I print the array element of a Bash array on separate lines? This one works, but surely there is a better way:

$ my_array=(one two three) $ for i in ${my_array[@]}; do echo $i; done one two three 

Tried this one but it did not work:

$ IFS=$'\n' echo ${my_array[*]} one two three 

Try doing this :

$ printf '%s\n' "${my_array[@]}" 

The difference between $@ and $*:

  • Unquoted, the results are unspecified. In Bash, both expand to separate args and then wordsplit and globbed.
  • Quoted, "$@" expands each element as a separate argument, while "$*" expands to the args merged into one argument: "$1c$2c..." (where c is the first char of IFS).

You almost always want "$@". Same goes for "${arr[@]}".

Always double quote them!