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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:36:20+00:00 2026-06-12T08:36:20+00:00

I have the file Testfile, whit a register in every line, each register having

  • 0

I have the file “Testfile”, whit a register in every line, each register having some fields:

foo1=32=test01=30/03/2012 10:03 p.m
foo2=54=test02=30/03/2012 10:05 p.m
foo3=912=test03=30/03/2012 10:08 p.m

(“=” is the field delimiter)

I read the first field for every register and store them in an array, and the same for the second field of every register:

NAMES=(`cut -f1 -d'=' Testfile`)
VALUES=(`cut -f2 -d'=' Testfile`)

That gives me two arrays like these: (foo1 foo2 foo3) and (32 54 912)

Is it possible to dynamically create variables named foo1 foo2 and foo3, and assign them their respective values?

I could do:

foo1 = ${VALUES[0]}
foo2 = ${VALUES[1]}
foo3 = ${VALUES[2]}

But I want to get the names of the variables dynamically, regarding the names that I’ve got from the file.

  • 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-12T08:36:21+00:00Added an answer on June 12, 2026 at 8:36 am
    cat Testfile 
    foo1=35
    foo2=54
    foo3=912
    
    for v in $(cat Testfile); do declare "$v"; done
    
    echo $foo1 
    35
    
    echo $foo2 
    54
    

    that will work if lines in file looks exact like this:

    var=val
    

    and here is a version that works with spaces:

    cat Testfile
    foo1 = 35
    foo2 = 54
    foo3 = 912
    
    while read line; do
        if [ -z "$line" ]; then continue; fi
        var=$(echo $line | cut -d'=' -f1)
        val=$(echo $line | cut -d'=' -f2)
        declare "$(echo $var)=$(echo $val)"
    done < Testfile
    
    echo $foo1 
    35
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: File.foreach(testfile.txt) { |line| print line } lines = File.readlines(testfile.txt) lines.each
I have a test file that has many lines, each line looks something like:
I have a very simple file watcher class which checks every 2 seconds if
I have a XMLDocument like: <Folder name=test> <Folder name=test2> <File>TestFile</File> </Folder> </Folder> I want
I have a varable var file = "testfile.txt"; I am using the following script
So I have testfile which contains Line one Another line and this is the
I have a problem, when I want get some data from CSV file to
I have a test file that contains tests taking quite a lot of time
I have a test file that contains 1,2,3 2,3,4 5,6,7 I want to insert
If I have a a textfile say file.txt and it contains random words like:

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.