-1

Say I've got the following array:

items[0]=0

items[1]=1

items[2]=2

etc.

Using echo/printf, how would I output only specific cells, for instance only cells 0,2,4. So far I've got this:

echo "${items[*]}"

This outputs all of the array, but how would I alter this line to make it only output the specified cells? I'm pretty sure I'm just missing what the syntax for this is.

Edit: Sorry, I inaccurately described what I'm asking. What I need to find out, is how to output the specified data point by calling the array only once.

For instance:

echo ${items[magical code stuff here]magical code stuff here too maybe}"

  • This has nothing to do with `ssh`. It's purely a shell issue (presumably `bash`). – chepner Nov 18 '21 at 23:58
  • bash has a simple way to print a slice (a contiguous group of elements) of an array (see [this answer](https://stackoverflow.com/questions/1335815/how-to-slice-an-array-in-bash/1336245#1336245)), but no simple way to print a non-contiguous list of elements. BTW, you should almost never use `[*]`, since it mashes the elements together into a single string. Use `"${array[@]}"` to keep them distinct. – Gordon Davisson Nov 19 '21 at 00:19

1 Answers1

-1

Replace the * with 0 2 4:

echo "${items[0]} ${items[2]} ${items[4]}"
Andrew
  • 1
  • 4
  • 19