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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:11:12+00:00 2026-06-15T19:11:12+00:00

My script need to store in a structure the result of a query: #!/bin/bash

  • 0

My script need to store in a structure the result of a query:

 #!/bin/bash
 user="..."
 psw="..."
 database="..."
 query="select name, mail from t"
 customStructure=$(mysql -u$user -p$psw $database -e "$query";)

I’ve no idea how store the array of {name, mail} from query result..

I need structure like this:

 array=[ [name1,mail1] , [name2,mail2], ....., [nameN, mailN] ]

Is there a way to do this in bash?

  • 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-15T19:11:13+00:00Added an answer on June 15, 2026 at 7:11 pm

    Bash arrays are initialized like so:

    myarray=("hi" 1 "2");
    

    To capture the individual portions of output of a command into an array, we must loop through the output, adding it’s results to the array. That can be done like so:

    for i in `echo "1 2 3 4"`
    do
        myarray+=($i)
    done
    

    In your example, it looks like you wish to get the output of a MySQL command and store the parts of it’s output lines into subarrays. I will show you how to capture lines into arrays, and given that, you should be able to figure out how to put subarrays into that yourself.

    while read line
    do 
        myarray+=("$line")
    done < <(mysql -u${user} -p${psw} ${database} -e "${query}")
    

    It’s also worth mentioning that for this kind of MySQL operation, where you don’t need output metadata (such as pretty formatting and table names), you can use MySQL’s -B option to do ‘batch output’.

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

Sidebar

Related Questions

In shell script i need to redirect output from dd command to /dev/null -
I have a bash script which need to execute some php scripts and to
I need to store data via a Perl CGI script on an Apache 2.x
Background: I need to store the following data in a database: osm nodes with
Below is my shell script that is working fine. #!/bin/bash DATE_YEST_FORMAT2=`perl -e 'use POSIX
Need to store some data in window.load and retrieve in document.ready : <script> $(window).load(function()
I have this script and need to be able to call the $play variable
I have a php script I need to run every 5 seconds (run, wait
I am trying to customize a script and need to get a POST value
I need to generate multiple databases to SQL, so I need script/program to automatically

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.