JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Reading a file into an array using Bash

Author:JIYIK Last Updated:2025/03/22 Views:

This article shows some ways to read lines from a file and load them into an array using Bash.

First, we'll look at the readarray method. Afterwards, we'll look at a more general method for reading files into Bash arrays.


Read a file into an array using Bash using the readarray method

readarray is a function that comes with Bash 4.0. This method works with all Bash versions higher than 4.0.

If you want to see which version of Bash you are currently running, you can use the following command:

echo ${Bash_VERSION}

If your Bash version is lower than 4.0, you can skip to the next method since readarray won't work for you.

We will use a file called numbers.txt for all examples with the following content.

numbers.txt file:

1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1

The syntax for reading this file and saving its contents into an array is as follows:

readarray -t Arr < numbers.txt

Breaking down the syntax, we have readarray as the name of the command, -t will remove newlines, and Arr is the name of the array into which the file contents will be written. numbers.txt is the name of the file we want to read.

请注意, if the file you want to read is not in the same folder as your script, you should provide the full path to the file.

The contents of this array can be read, as shown in the following example:

echo ${Arr[1]}

The above code will output the line in array index 1, which is the second line of the file.

The output is:

2 1

To print out an entire array, use the following syntax:

echo ${Arr[@]}

The output of this will be:

1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1

Reading a file into an array using a generic method using Bash

The syntax of the general method is as follows:

IFS=$'\r\n' GLOBIGNORE='*' command eval  'ArrName=($(cat filename))'

Reading this array would have similar syntax as shown in the previous example:

echo ${ArrName[1]}

The Bash line above will display the contents of array index 1. However, if we want to display the contents of the entire array, we can use:

echo ${ArrName[@]}

General Method Description

ArrName in the general syntax is the name of the array, and the filename after cat is the file name to be read. We will modify these two variables for our custom use.

IFSDefines the break characters, in this case \r and \n . \r is a carriage return character and \n is a line feed character.

On most modern systems you'll see \n used as a newline character.

GLOBIGNORE='*'is set to be safe to avoid weird edge cases with file names. It should be noted that due to the use of GLOBIGNORE, this command requires Bash (and may not necessarily work with other shells such as zsh or fish).

Another important point to note is that this solution is much slower than readarray . The only reason to use it is if your version of Bash is older than 4.0.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

String comparison in batch files

Publish Date:2025/03/22 Views:167 Category:OPERATING SYSTEM

A string is an ordered collection of characters. You can compare strings using conditional commands in batch files, namely, if, if-else, and for commands. Strings may contain spaces and special characters, which can cause errors in the batc

Remove double quotes from variable in batch file

Publish Date:2025/03/22 Views:177 Category:OPERATING SYSTEM

In batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we do not want to see these quotes in the output. These quotes can be removed from the variables in batch files. There are many wa

Reading file into variable in batch script

Publish Date:2025/03/22 Views:120 Category:OPERATING SYSTEM

Sometimes, we need to put the entire contents of a file into a variable for various purposes, such as finding specific data from a file, replacing a specific part of a file, etc. In Batch, it is very easy to put the entire file contents in

Print a file after skipping first X lines in Bash

Publish Date:2025/03/22 Views:186 Category:OPERATING SYSTEM

Suppose you have a file, a large file, and you want to display its contents. How would you do it? You obviously don't want to print out the entire contents of the file, as that's not very practical. You might want to print some selective li

Get the Primary IP Address in Linux

Publish Date:2025/03/22 Views:70 Category:OPERATING SYSTEM

There are various ways to get network details in Linux. We will learn some of them in this article. This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash

Bash History Size

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it. Most deve

Find Current Folder Name in Bash

Publish Date:2025/03/22 Views:106 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Changing Directories in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

In this article, we will learn how to change directories in Bash. The term directory is used to refer to a folder. Because you frequently move between folders when using Bash and when using the Git version control system, it is essential to

Running Regular Expressions in Case Statements in Bash

Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM

This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial