-1

This is my file

1 2 3
4 5 6
7 8 9

Following is my code

i=0;
while read line;do
   arry_$i="${line[@]}";
   i=$(($i+1));
done < file.txt
IFS=' ';eval "arr_00=($arr_0)"
IFS=' ';eval "arr_11=($arr_1)"
IFS=' ';eval "arr_22=($arr_2)"

Following is the output

arry_0=1 2 3: command not found
arry_1=4 5 6: command not found
arry_2=7 8 9: command not found

When I try to assign file each line to variable noticed an "command not found"

Thank you for suggestions

Jaff_NL
  • 73
  • 5
  • 2
    That isn't how you define or assign to arrays in bash unfortunately. Maybe this would help. https://tldp.org/LDP/abs/html/arrays.html – daidoji70 Jul 04 '23 at 13:49
  • Also, if you want to store a value with spaces in a variable in bash its a little tricky (most bash parsing is done with whitespace). `arry_"$i"="\"${line[@]}\""` might work or just `arry_"$i"="\"$line\""` – daidoji70 Jul 04 '23 at 13:54
  • See https://unix.stackexchange.com/a/131767 – daidoji70 Jul 04 '23 at 13:55
  • With these changes I am getting same error ``` while read line;do arry_"$i"="\"${line[@]}\""; i=$(($i+1)); done < file.txt arry_9="1 2 3": command not found arry_10="4 5 6": command not found arry_11="7 8 9": command not found ``` – Jaff_NL Jul 04 '23 at 13:58
  • Now I have edited the question – Jaff_NL Jul 04 '23 at 14:18
  • 1
    When you write `${line[@]}`, you treat `line` as if it were an array. However, `line` is a scalar. This can't be right. – user1934428 Jul 04 '23 at 14:33
  • Furthermore in `arry_$i=....`, since you have a parameter expansion to the left of the equal sign (the `$i` part), the line is interpreted as a command execution, not as an assignment. Hence the error message _command not found_. Don't do any quoting or parameter expansion to the left of the equal sign in an assignment! – user1934428 Jul 04 '23 at 14:38
  • check your code (including `#!/bin/bash` as the first line) at https://shellcheck.net . Fix those issues and if that doesn't solve your problem, update your question with corrected code. Good luck. – shellter Jul 04 '23 at 14:44
  • 2
    please update the question with the expected results; do you want 3 variables where each variable is a string (eg, `arry_0='1 2 3'`) or should those 3 variables represent arrays (eg, `arry_0=([0]="1" [1]="2" [2]="3")` or are you expecting something else? – markp-fuso Jul 04 '23 at 15:06
  • 1
    You can make the loop do what (I think) you want to do by replacing it with `while read -r -a "arry_$i"; do i=$((i+1)); done – pjh Jul 04 '23 at 15:45
  • Use `readarray` instead of a loop to load the lines of a file into an array. – Shawn Jul 04 '23 at 17:50

1 Answers1

1

Just addressing the error message:

When you do:

   arry_$i="${line[@]}";

the shell does not recognise this as a variable assignment as it does not match the form name=[value] because arry_$i is not a valid form for a name.

After this it performs the various expansions, word-splitting, quote-removal, etc. On the first loop iteration, this results in a word arry_0=1 2 3.

This word is treated as a command to run. This gives your error message.


While strongly advising that you do not do the following, you could fix the problem by making the variable name generation happen before you attempt to assign to it:

while read line; do
   declare -n var=arry_$i
   var=$line;
   ((i++))
done < file.txt

Note that since line is a simple variable and not an array, ${line[@]} == ${line[0]} == $line.

I'm not sure what the purpose of your exec is. For the sample data shown, it would be fine to directly execute: arry_00=($arry_0), etc

jhnc
  • 11,310
  • 1
  • 9
  • 26