-2

I get 2 arrays using curl I user readarray <<<$(curl http://etc)

So for example I got first array first line "Best friends" and second array first line "John Jim Piter"

How I can treat each line of the first array as new_array_name and fill it from the second array using bash? Each line of first array must be treat as separated array name and fill from each line of the second array.

Where are multiple line in each array

UPD. As @choroba mention also I need replace whitespace in variable name

I have 2 arrays: declare -a my_array=([0]=$'BEST FRIENDS' [1]=$'Second line')

and 2nd array = declare -a my_array2=([0]=$'JOHN Jim Piter' [1]=$'This is the test') I need treat each line from first array as variable name and each line from second array as value. Like this - BEST FRIENDS=JOHN Jim Piter

  • 2
    `Best friends` is not a valid variable name. – choroba Feb 10 '22 at 14:06
  • 1
    please update the question with sample inputs (output from running `typeset -p ` for both of your input arrays) plus the expected results (if also arrays then show what you would expect from running `typeset -p` against said array(s)) – markp-fuso Feb 10 '22 at 14:13
  • 1
    To echo what choroba and markp-fuso are saying above -- an English-language description of your desired behavior is a lot harder to work with than concrete sample input and output _showing_ your desired behavior. Nobody wants to do the work to write and test an answer only to find out when they're done that they misunderstood the question. – Charles Duffy Feb 10 '22 at 14:21
  • (That's part of the value of `declare -p arrayname`: You can show it to us for your input arrays, and also for a manually-constructed version of your desired output array, thus providing a clear and concrete example of desired behavior). – Charles Duffy Feb 10 '22 at 14:22
  • fwiw ... `declare -p` and `typeset -p` provide the same info; `typeset` is the 'older' command and nowadays referred to as a synonym for `declare` – markp-fuso Feb 10 '22 at 14:31
  • @markp-fuso typeset first array - `declare -a my_array=([0]=$'BEST FRIENDS\n' [1]=$'Second line\n')` and 2nd array = `declare -a my_array2=([0]=$'JOHN Jim Piter \n' [1]=$'This is the test \n')` I need treat each line from first array as variable name and each line from second array as value. Like this - `BEST FRIENDS=JOHN Jim Piter ` – stevemayster Feb 10 '22 at 14:36
  • @stevemayster, enough information to answer (or evaluate answers for correctness) should be _in the question itself_; content that's only in comments doesn't count, so use the [edit] button. And if you use the `-t` argument to `readarray` you won't have the `$'\n'`s on the end. – Charles Duffy Feb 10 '22 at 14:37
  • @stevemayster, ...beyond that, I thought you said you needed to remove spaces, but you're showing sample output with `BEST FRIENDS=` with the space still there; with the space, it's not a valid variable name. (I also thought you said you wanted your output to be an array; `'JOHN Jim Piter'` isn't an array, it's a string -- an array would be `best_friends=( JOHN Jim Piter )`). – Charles Duffy Feb 10 '22 at 14:39
  • Mind, you _can_ use spaces in names if you're storing everything in an "associative array" -- what other languages call a map. – Charles Duffy Feb 10 '22 at 14:40
  • You showed us your inputs -- that's good -- but show us your desired outputs not as a string but as a bash data structure; doing so will force you to decide on something that's actually valid or possible (so you can't be asking for, say, a variable name with a space in it, unless it's an associative array key). – Charles Duffy Feb 10 '22 at 14:44
  • Just to underline it, it's not the lower-case characters in `Best friends` that made it an invalid name -- you're _supposed_ to use lower-case characters in shell variables you define yourself, the all-caps ones are for variables with special or internal meaning to the shell itself; it's the space that's the problem, so `BEST FRIENDS` is just as invalid. – Charles Duffy Feb 10 '22 at 14:45
  • ...btw, https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html is the standards document describing how all-caps is the namespace used for variables with special meaning to the shell itself. (When reading it, keep in mind that setting a regular shell variable overwrites any like-named environment variable). – Charles Duffy Feb 10 '22 at 14:46

1 Answers1

0

This combines several of techniques, each of which is already a duplicate of something already in our knowledge base.

#!/usr/bin/env bash
array1=( "Best Friends" "Worst Enemies" )
array2=( "Jim John Pieter" "Alfred Joe" )

for idx in "${!array1[@]}"; do
  varname=${array1[$idx]//" "/}              # remove spaces from target name
  read -r -a "$varname" <<<"${array2[$idx]}" # read into target
  declare -p "$varname" >&2     # show what we did
done

This creates two arrays, as follows:

declare -a BestFriends=([0]="Jim" [1]="John" [2]="Pieter")
declare -a WorstEnemies=([0]="Alfred" [1]="Joe")

...which, being arrays, can be used as:

for friend in "${BestFriends[@]}"; do
  echo "I sure do enjoy hanging with $friend!"
done
Charles Duffy
  • 257,635
  • 38
  • 339
  • 400
  • Thanks for script. But I need to use Russian symbol in variable name and I think it's not possible. If I set `varname=тест` I got an error `bash: read: тест': not a valid identifier. I think I'm failed) – stevemayster Feb 10 '22 at 15:19
  • 1
    As I recommended before, you should use associative array keys instead of regular variables. (This is part of why I urged you to show valid output as part of the question, to force you to decide on what valid output _should be_ instead of asking us for something impossible) – Charles Duffy Feb 10 '22 at 15:21
  • 1
    At initialization time, `declare -A vars=( )`; then, inside the loop, `vars[${array1[$idx]}]=${array2[$idx]}`; when done, `declare -p vars` to look at what you've done, and you can look up `vars["Best Friends"]` to retrieve the relevant values (though they'll need to be a string, not an array). – Charles Duffy Feb 10 '22 at 15:23
  • maybe I don't understand you right `for idx in "${!array1[@]}"; do vars[${array1[$idx]}]=${array2[$idx]}; done`. I got `bash: Best Friends: syntax error in expression (error token is "Friends")` . I'm dumb as *uck. I forget to do `declare -A vars=( )` – stevemayster Feb 10 '22 at 15:33