How to use string in array name?

Hi,
I'm the beginner in bash scripting and I just want to create loop, which will create a few tables with string in name, and each time i try to do this in the following way, I receive:

a=1; 
while [ $a -ne 5 ] 
    do 
    echo "Plik a=$a"
    for m in {1..4} 
        do echo "Plik m=$m"
        table_$a=(`grep pattern-filename-$m*`)
    done
    ((a+=1))
done
bash: syntax error near unexpected token ``grep pattern-filename-$m*`' 

Please help me :slight_smile:

table_$a=$(grep pattern-filename-$m*)

Try to avoid using backtics `code` , its hard to read, and nesting needs escaping. Do use $(code) instead.

Now I did not receive this error message, but for following code:

a=1; 
while [ $a -ne 3 ] 
    do 
    echo "Plik a=$a"
    for m in {1..2} 
        do echo "Plik m=$m"
        table_$a=$(ps -ef | grep bash | awk '{print $2}' | head -5)
    done
    ((a+=1))
done

where the output for command ps -ef | grep bash | awk '{print $2}' | head -5 is

533
1192
8195
16404
16437

I get output:

Plik a=1
Plik m=1
bash: table_1=533: command not found
Plik m=2
bash: table_1=533: command not found
Plik a=2
Plik m=1
bash: table_2=533: command not found
Plik m=2
bash: table_2=533: command not found

So sth is wrong...
For now I'm only interested in getting all numbers into each table_$a.

You don't. Shell doesn't do that kind of doublethink, as a security feature. Neither do most languages. You can brute force it with eval but that will be ugly, and a security hole.

Some like ksh, awk, or perl, can do something similar through associative arrays like table["asdf"]="qwerty". But what are you actually trying to do? Why do you need randomly named variables?

Hi,
I've got two files randomly named with common begin and different number (eg. filename1, filename2).
Each filename* contains lines with random number. Now I want to create table (with name the same as file) per file in one loop.

That's just repeating that you want variable names picked at runtime. I'm asking why you need variables named at runtime. I'm not sure how you'd even use those dynamically named arrays. You've just found out that shell doesn't do that kind of doublethink easily, you'd probably have to resort to ugly and unsafe eval tricks.

What are you going to be doing with these tables of data? If you need sequential access, that's easy to do without dumping the whole file into memory.

The whole idea of program I want to use is to do:

1) For a randomly named files (eg, fileName1, fileName223) grep lines that include pattern, then cut the third column of each line.

So now, output will be:
table1= (1034 2345)
table223=(2312 4567)
....
2) Each element of table* I want to use to grep all lines with them {( (1034 2345) and (2312 4567)} from source files (eg, fileName1, fileName223)
note: I want to grep them separately (1034 2345 from fileName1; 2312 4567 from fileName223)

This is the final result for now, out will be:
1034 file with all lines includes this code(1034) in file fileName1
2345 file with all lines includes this code(2345) in file fileName1
2312 file with all lines includes this code(2312) in file fileName223
4567 file with all lines includes this code(4567) in file fileName223

Hope this is clear for you.

It's not at all clear why you insist on storing them instead of just printing them in the first place, no. The arrays appear to have no function, since you're not comparing them to each other.

If you are comparing them to each other in any way, your example doesn't demonstrate this.

awk '/pattern/ { print $3, "file with all lines includes this code("$3") in file", FILENAME }' file1 file2 file3 ...