Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Arrays in the Korn shell (and Bash and others) are sparse, so if you use <code>unset</code> to delete members of the array, you won't be able to use the size of the array as an index to the last member and other limitations.</p> <p>Here are some useful snippets (the second <code>for</code> loop is something you might be able to put to use right away):</p> <pre><code>array=(1 2 3) unset array[2] echo ${array[2]} # null indices=(${!array[@]}) # create an array of the indices of "array" size=${#indices[@]} # the size of "array" is the number of indices into it size=${#array[@]} # same echo ${array[@]: -1} # you can use slices to get array elements, -1 is the last one, etc. for element in ${array[@]}; do # iterate over the array without an index for index in ${indices[@]} # iterate over the array WITH an index do echo "Index: ${index}, Element: ${array[index]}" done for index in ${!array[@]} # iterate over the array WITH an index, directly </code></pre> <p>That last one can eliminate the need for a counter.</p> <p>Here are a couple more handy techniques:</p> <pre><code>array+=("new element") # append a new element without referring to an index ((counter++)) # shorter than ((counter=counter+1)) or ((counter+=1)) if [[ $var == 3 ]] # you can use the more "natural" comparison operators inside double square brackets while [[ $var &lt; 11 ]] # another example echo ${array[${index}-1] # math inside an array subscript </code></pre> <p>This all assumes ksh93, some things may not work in earlier versions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload