Skip to main content

Use Read Command to Get User Inputs Into an Array in Bash

Learn how to use the read command to get the user input into an array in bash scripts.

Sagar Sharma

There are multiple ways to insert values in the array but most of them are manual ones.

But, adding values manually is not always a good idea especially when one wants to add hundreds of elements in one array.

And in that case, you can read into an array where you can use the file or a line of multiple strings to add values to an array.

So let's have a look at how you can read into an array in bash.

How to read into an array in bash

To read into an array, you use the read command with the -a flag.

It uses the whitespace as a delimiter (by default) so make sure to separate two keywords by whitespace so bash can differentiate between two elements and store them in an array with different indexes.

The syntax to read into an array is pretty simple. All you have to do is use the read command with the -a flag and append the name of the array:

read -a ArrayName

And it will ask the user to enter the values that need to be stored in the specified array.

Now, there are two ways to print the elements of an array:

  • Using echo ${ArrayName[@]}
  • Using echo ${ArrayName[*]}

But there's a difference between how they deal with the elements:

  • The [@] method will print values as they are individual elements
  • Whereas the [*] method will print all the elements as a single string

Now, let's have a look at some examples.

1. Enter values in the prompt

If you want to add multiple values in an array all at once, then, you can refer to this method where the user will be asked to enter the values one by one.

And to do so, you'd have to use the following syntax:

echo "Enter whitespace-separated values:"
read -a array
echo "You've entered the following elements: ${array[@]}"

Here, I've used an echo command that simply prints "Enter whitespace-separated values:" and the read command will ask the user to input values.

And the last echo command will print the value of the array to verify whether all the elements are there or not.

If you were to execute the above script, then, you should expect the following output:

read into array by user interaction in bash

But what if you want to print a specific element, then, you have to specify the index number of that element in array[@] by replacing the @ with the index number.

For example, if I want to print the 5th element of the array, then, I will be using the following:

echo "Enter whitespace-separated values:"
read -a array
echo "The 5th element in an array is: ${array[5]}"
print specific element from array

2. Read into an array using a file (using a loop)

If you have hundreds of strings already present in the form of a file then, you can use this method.

This method involves the usage of the while loop in the bash so if you want a method without using any loops, then you can skip to the next solution.

To use this method, you can follow the given syntax:



while IFS= read -r line; do
done < "$file"

echo "All the elements of an array are: ${array[@]}"

I know it looks complex. Let me break it down for you.

  • array() created an empty array called array.
  • IFS= is used to specify a delimiter (which is whitespace in my case).
  • array+=("$line") the line variable stores the value of each line in the file and using this argument, it gets stored in an array.
  • done < "$file" instructs the loop to read the filename from the $file using the input redirection <.
Before you use the above script, make sure you have a file named data.txt in the same directory.

And when executed, you can expect the following result:

read into an array using file

3. Read into an array using a file (without loop)

Like me, if you try to avoid loops as much as possible then you can refer to this section which is quite easy compared to the above method.

Here, I will be using the mapfile command as shown:



mapfile -t array < "$file"

printf "Entered elements are:\n" 
printf '%s\n' "${array[@]}"

Here, the -t flag is used to remove the tailing of lines such as whitespaces to have consistent formatting.

Whereas the '%s\n' is used to print each character of an array in a new line.

This is what I got while running the above script:

read into an array from file without loop

And there you have it!

Bash beginner? Let me help you out

If you are just getting stared with bash and are confused between multiple course, then, we made a simple series of bash articles that would clear all the basics:

Bash Tutorial Series for Beginners: Start Learning Bash Scripting
The 10 chapter series will teach the bash scripting essentials.

I hope you will find this guide helpful.