0

Doing some formatting fun, playing with xargs and how it passes data to scripts etc, and I'm having a little trouble with creating an array.

alias lstest='ls | xargs --delimiter="\n" ~/.config/lsdif_color.sh'

for fileLine in "$@"
do
    if [[ $fileLine == total* ]]; then echo $fileLine;
    else 
        cols=($fileLine);
        echo ${#cols}, $l
        echo ${cols[0]}
        echo ${cols[$(( ${#cols}-1 ))]}
    fi;
done 

So, this works fine. The "cols" array shows the right length and it outputs the first and last element perfectly.

Now for the problem:

...
else
  # Method 1
  declare -a cw=(11 2 8 7 6 5 4 3 6 0);
  echo ${#cw}

  # Method 2
  widths=($(11 2 8 7 6 5 4 3 6 0));
  echo ${#widths}

  #Method 3
  cws[0]="11"
  cws[1]="2"
  cws[3]="8"
  cws[4]="7"
  cws[5]="6"
  cws[6]="5"
  cws[7]="4"
  cws[8]="3"
  cws[9]="6"
  cws[10]="0"

  echo ${#cws}

fi;

in all three method's of attempting to define these arrays, the echoed length is always 2. Not 10. The Cols array from above lists 10 elements, but attempting to create an array of numbers fails, even when i'm letting them be strings.

Why?

Thanks Jaeden "Sifo Dyas" al'Raec Ruiner

  • I have successfully used declare -a myarray; myarray+=("val1"); myarray+=("val2") etc. – DopeGhoti Dec 11 '15 at 21:30
  • ${cols[-1]} will also get you the last element. – glenn jackman Dec 11 '15 at 23:31
1

You just have to use:

echo {#cws[@]}
  • {#cws} is normally used to return a string's length
  • the [@] is important, because without this, the value of $cws is not the whole array, it instead defaults to first element of array's value, aka $cws[0]

For example on our command prompt we can do a simple three-element array similar to yours:

$ cws[0]="11"; cws[1]="2"; cws[3]="8"

Now look what happens when we refer to just $cws:

$ echo $cws
11

So, when using bash expansion to determine length of something, {# ... }, without the [@], you write ${#cws}, $cws is 11, so you are asking bash "what is the length of 11"? and of course bash looks at the string 11 which has two characters, so Bash says `Oh it is 2":

$ echo ${#cws}
2

So, that is why to specify you meant the length of the array, remember the [@]:

$ echo ${#cws[@]}
3
| improve this answer | |
  • I am an idiot, or just code blind. I was sure I had put the @ in the string but obviously I hadn't. the joys of teaching oneself a new language. Like all those times in C we forgot == instead of just =. Thanks for the pointer. :) - J"SD"a'RR – JaedenRuiner Dec 14 '15 at 14:36
1

The problem is in how the arrays are referenced. To begin:

$ declare -a cw=(11 2 8 7 6 5 4 3 6 0)

Now, let's look at the length two different ways:

$ echo ${#cw}
2
$ echo ${#cw[@]}
10

The first method is returning the length of the element of indice 0 (in this case it also happens to be first element of cw). The second method, by contrast, is returning the number of elements in the array.

More

In the question, there is the code:

widths=($(11 2 8 7 6 5 4 3 6 0));

This code tries to execute a command called 11 with arguments 2 8 7 6 5 4 3 6 0. The result depends on whether there is a command called 11. If there isn't, you will see:

bash: 11: command not found
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.