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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:28:11+00:00 2026-05-29T09:28:11+00:00

I am writing a bash script where I need to create arrays inside a

  • 0

I am writing a bash script where I need to create arrays inside a for loop and name each array using the string contained in the counter (an array element) each iteration.

Here is the code:

myArray=( joe bob dave mark )


for i in "${myArray[@]}"
do
  "$k_array"=( `cat fileUsedToPopulate.txt` )
done

# echo to test if one of the arrays has been created and populated

for j in "${joe_array[@]}"
do
  echo $j
done

The desired result is 4 arrays, joe_array bob_array etc, each populated with the file.

However I haven’t found anyway to escape the $k so that it appends to the array name when declaring/populating it.

Here is the error I am getting:

line 30: syntax error near unexpected token `cat fileUsedToPopulate.txt‘
line 30: "$k_array"=(cat $DIR/$braDir/oem.txt` )’

Thanks for any help provided.

  • 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-29T09:28:14+00:00Added an answer on May 29, 2026 at 9:28 am

    Use eval to assign to variable whose name depends on another variable:

    val=$(cat fileUsedToPopulate.txt)
    for k in "${myArray[@]}"
    do
      eval "${k}_array"="\"${val}\""
    done
    

    Note that you need nested double quotes (with proper escaping) if the contents of the file contain a space or other separator. Otherwise shell is going to interpret part of it as a command to execute. Also, you need braces around variable name in $k or the shell will look for a variable names k_array.

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

Sidebar

Related Questions

I'm writing a Bash script where I need to pass a string containing spaces
I'm writing a script for Bash and I need to get the name of
Im writing a bash-script to perform an offsite backup, using rsync over SSH. I'm
I'm writing a Bash script. I need the current working directory to always be
I'm writing a bash script that needs to loop over the arguments passed into
I am writing a bash script where I need to clone a git rep.
I am writing a script in bash on Linux and need to go through
I am writing a bash script which accept parameters. I am using getopts to
I am writing a bash script to deal with some installations in an automated
I'm writing a bash script that needs to delete old files. It's currently implemented

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.