| Start |
|---|
| Array Assignments List Assignment If you are familiar with Perl, C, or Java, you might think that Bash would use commas to separate array elements, however this is not the case; instead, Bash uses spaces: Create an array with new elements: Subscript Assignment Create an array with explicit element indices: Assignment by index Assignment by name (associative array) Dynamic Assignment Create an array from the output of other command, for example use seq to get a range from 1 to 10: Assignment from script's input arguments: Assignment within loops: where |
| Accessing Array Elements Print element at index 0 Print last element using substring expansion syntax Print last element using subscript syntax Print all elements, each quoted separately Print all elements as a single quoted string Print all elements from index 1, each quoted separately Print 3 elements from index 1, each quoted separately String Operations If referring to a single element, string operations are permitted: so |
| Array Modification Change Index Initialize or update a particular element in the array Append Modify array, adding elements to the end if no subscript is specified. Replace the entire array with a new parameter list. Add an element at the beginning: Insert Insert an element at a given index: Delete Delete array indexes using the Merge This works for sparse arrays as well. Re-indexing an array This can be useful if elements have been removed from an array, or if you're unsure whether there are gaps in the array. To recreate the indices without gaps: |
| Array Length
This works also with Strings in single elements: |
| Array Iteration Array iteration comes in two flavors, foreach and the classic for-loop: You can also iterate over the output of a command: |
| Associative Arrays Declare an associative array Declaring an associative array before initialization or use is mandatory. Initialize elements You can initialize elements one at a time as follows: You can also initialize an entire associative array in a single statement: Access an associative array element Listing associative array keys Listing associative array values Iterate over associative array keys and values Count associative array elements |
| Looping through an array Our example array: Using a Using C-style Using Using Using an Using an |
| Destroy, Delete, or Unset an Array To destroy, delete, or unset an array: To destroy, delete, or unset a single array element: |
| List of initialized indexes Get the list of inialized indexes in an array |
Array from string Each space in the string denotes a new item in the resulting array. Similarly, other characters can be used for the delimiter. |
| Reading an entire file into an array Reading in a single step: Reading in a loop: Using |
| Array insert function This function will insert an element into an array at a given index: Usage: Example: |
Read Article
https://develop.horje.com/learn/1434/reference