Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:22:27+00:00 2026-05-31T15:22:27+00:00

I would like to read a file into a script, line by line. Each

  • 0

I would like to read a file into a script, line by line. Each line in the file is multiple values separated by a tab, I’d like to read each line into an array.

Typical bash “read file by line” example;

while read line
do
echo $line;
done < "myfile"

For me though, myfile looks like this (tab separated values);

value1 value2 value3
value4 value5 value6

On each iteration of the loop, I’d like each line to go into an array so I can

while read line into myArray
do
 echo myArray[0]
 echo myArray[1]
 echo myArray[2]
done < "myfile"

This would print the following on the first loop iteration;

value1
value2
value3

Then on the second iteration it would print

value4
value5
value6

Is this possible? The only way I can see is to write a small function to break out the values manually, is there built in support in bash for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:22:28+00:00Added an answer on May 31, 2026 at 3:22 pm

    You’re very close:

    while IFS=$'\t' read -r -a myArray
    do
     echo "${myArray[0]}"
     echo "${myArray[1]}"
     echo "${myArray[2]}"
    done < myfile
    

    (The -r tells read that \ isn’t special in the input data; the -a myArray tells it to split the input-line into words and store the results in myArray; and the IFS=$'\t' tells it to use only tabs to split words, instead of the regular Bash default of also allowing spaces to split words as well. Note that this approach will treat one or more tabs as the delimiter, so if any field is blank, later fields will be “shifted” into earlier positions in the array. Is that O.K.?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read a text file and input its contents into an
In java i would like to read a file line by line and print
I have a byte array in memory, read from a file. I would like
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
I would like to know how is possible to read a file placed in
I would like to save all error_reporting's notices into a file so that it
I need to use bash to insert a line into a python file. This
In Perl, I'm trying to read a file line by line and process each
I would like to read the text and binary attachments in a saved Outlook

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.