Using arrays in bash

Hi,
I am trying to implement a simple bubble sort program in bash. However, I seem to be facing some problems while operating on arrays. So far, I have been using the Advanced Bash Shell Guide on tldp.org as my reference, but even the syntax given there does not seem to work. I would be grateful if someone here in this forum could try to look into this matter:

19 if ${array"$comp_pos"]} -lt ${array"$j"]} then
20 temp = ${array${comp_pos}]}
21 {array${comp_pos}]} = ${array${j}]}
22 {array${j}]}=$temp
23 fi

Thanks for going through this post.

I am getting the following error message:

8.sh: line 20: temp: command not found
8.sh: line 21: {array[2]}: command not found
8.sh: line 22: {array[1]}=: command not found
8.sh: line 20: temp: command not found
8.sh: line 21: {array[3]}: command not found
8.sh: line 22: {array[2]}=: command not found

temp="${array${comp_pos}]}"

There should be no spaces between the variable name and its values, so temp = somevalue is not correct. The above in bold is so change all accordingly (if you have others too)

Also, the if conditional is missing a closing square bracket and semi-colon at end… correct is (or one of the correct ways is): if … ]; then

Further, an array is called with ${value[pos]} while assigning an array is value[pos]=

I am afraid most of the error messages have nothing to do with the usage of arrays. The are just simple errors you made.

temp = ${array${comp_pos}]}

This means calling the command *temp *with two parameter fields:

  1. =
  2. what ${array${comp_pos}]} becomes after bash did all substitutions.
    What you probably want is:
temp=${array${comp_pos}]}

This means: asign what ${array${comp_pos}]} becomes after substitution to the variable temp.

{array${comp_pos}]} = ${array${j}]}

Again first the shell does some substitutions: it will e.g. substitute ${comp_pos} with 2 and I do not know what it will substitute *${j} *with and after that ${array[jresult]}. But the result will be something like:

{array[2]} = someresult

Maybe by now you will see that you not only made the same mistake as above (spaces where not allowed), but also forgot a $ as the first character.
Orr as ana alternative (i do not know what you want to achieve exactly) you wanted the { } removed.

In short you should better read what you have written, and then ask yourself: how many fases will this go through and what happens in every of those phases. The man pages of bash come to help here. This involves things like: alias substitution, parameter substitution. …, program exececution.

I will leave the rest to you. You can of course come back with further questions, but I advise you first to write some simple scripts to train yourself and find out about the wonderfull (im)possibilities of bash :slight_smile:

In the additional info Henk provided above, it seems you don’t really seem to understand one-dimentional arrays in Bash. I’ll give a very simple example how to create and call them…

for i in {1..3}; do
    myvar[i]="value$i"
done

echo "${myvar[li]}"[/li]```


in the for loop, some values get assigned to the 'myvar' arrays and later on we print them with 'echo ${myvar[*]}'  (the * in the array is used to report all array values and one can also use @ instead of * but there are minor differences so you'll have to read up a bit). More examples you can find about arrays - [Bash Arrays | Linux Journal](http://www.linuxjournal.com/content/bash-arrays)

Yes, i have got it. As I said before, I was trying to perform a bubble sort, and the fragment of the code tries to swap values, in case the condition is satisfied. From what I gathered from your replies, and the links where I was referred, I found that this is what the code should be:

if  "${array"$comp_pos"]}" -gt "${array"$j"]}" ] ; then
                        temp=${array"$comp_pos"]}
                        array"${comp_pos}"]=${array"$j"]}
                        array"$j"]=$temp
                        #echo "$temp"
     fi

So many thanks, microchip8 and hcw for your kind advice and help. Your help really did the trick for me. Thanks once again!

No problemo, ask anytime… and what? Henk and me get no reputation points? :stuck_out_tongue:

Was away for one and a half day. Look if your programming style improved immensly (with all the nice colours). But still no reps >:)

What can I say, Henk? Unthankful cretins… why do we even bother? :stuck_out_tongue:

I agree, but maybe the place where to click for it should be a bit more eye catching. I did not realy understand what it was for until, by incident, I read something about in a post.

In any case, he said: “it works”. And he said: “thank you”. Much more then many do :slight_smile: