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 8210205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:59:46+00:00 2026-06-07T09:59:46+00:00

I am trying to read a file containing lines into a Bash array. I

  • 0

I am trying to read a file containing lines into a Bash array.

I have tried the following so far:

Attempt1

a=( $( cat /path/to/filename ) )

Attempt2

index=0
while read line ; do
    MYARRAY[$index]="$line"
    index=$(($index+1))
done < /path/to/filename

Both attempts only return a one element array containing the first line of the file. What am I doing wrong?

I am running bash 4.1.5

  • 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-06-07T09:59:48+00:00Added an answer on June 7, 2026 at 9:59 am

    Latest revision based on comment from BinaryZebra’s comment
    and tested here. The addition of command eval allows for the expression to be kept in the present execution environment while the expressions before are only held for the duration of the eval.

    Use $IFS that has no spaces\tabs, just newlines/CR

    $ IFS=$'\r\n' GLOBIGNORE='*' command eval  'XYZ=($(cat /etc/passwd))'
    $ echo "${XYZ[5]}"
    sync:x:5:0:sync:/sbin:/bin/sync
    

    Also note that you may be setting the array just fine but reading it wrong – be sure to use both double-quotes "" and braces {} as in the example above


    Edit:

    Please note the many warnings about my answer in comments about possible glob expansion, specifically gniourf-gniourf’s comments about my prior attempts to work around

    With all those warnings in mind I’m still leaving this answer here (yes, bash 4 has been out for many years but I recall that some macs only 2/3 years old have pre-4 as default shell)

    Other notes:

    Can also follow drizzt’s suggestion below and replace a forked subshell+cat with

    $(</etc/passwd)
    

    The other option I sometimes use is just set IFS into XIFS, then restore after. See also Sorpigal’s answer which does not need to bother with this

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

Sidebar

Related Questions

I'm trying to read a text file containing 20 names into an array of
I am trying to read a CSV file containing a line like following: test,"test,"test,test,test,test
Trying to compare input to a file containing alert words, read MYINPUT alertWords=( `cat
I have a file containing bash commands, and I read them one by one
I am trying to read a comma separated file through InputStream containing some Slovak
I've uploaded a ZIP file containing both the XML file I'm trying to read
I am trying to use Python to read in an XML file containing some
I'm trying to read a text file containing the name of the image, and
I'm trying to read a file containing filepaths line by line and scp the
I have created CSVReader and I am trying to read csv file from assets

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.