#] #] ********************* #] "$d_SysMaint"'Linux/array notes.txt' - ??? # www.BillHowell.ca ??Mar2023 initial # view in text editor, using constant-width font (eg courier), tabWidth = 3 #48************************************************48 #24************************24 # Table of Contents, generate with : # $ grep "^#]" "$d_SysMaint"'Linux/array notes.txt' | sed "s/^#\]/ /" # #24************************24 # Setup, ToDos, #08********08 #] ??Mar2023 #08********08 #] ??Mar2023 #08********08 #] ??Mar2023 #08********08 #] ??Mar2023 #08********08 #] 17Mar2023 search "bash arrays assign a value" Unix_Array[$counter]=$line +-----+ https://stackoverflow.com/questions/11087918/how-can-i-assign-a-value-to-an-array-in-bash How can I assign a value to an array in Bash? Asked 10 years, 9 months ago Modified 1 year, 4 months ago Viewed 38k times +--+ There are a few syntax errors here, but the clear problem is that the assignments are happening, but you're in an implied subshell. By using a pipe, you've created a subshell for the entire while statement. When the while statement is done, the subshell exits and your Unix_Array ceases to exist. In this case, the simplest fix is not to use a pipe: counter=0 while read line; do Unix_Array[$counter]=$line; let counter=counter+1; echo $counter; done < hello.txt echo ${Unix_Array[0]} echo ${Unix_Array[1]} echo ${Unix_Array[2]} By the way, you don't really need the counter. An easier way to write this might be: $ oIFS="$IFS" # Save the old input field separator $ IFS=$'\n' # Set the IFS to a newline $ some_array=($(> Interesting - I'll wait untill I need it +-----+ https://linuxize.com/post/bash-arrays/ Bash Arrays Updated Nov 22, 2019 Bash Arrays Bash supports one-dimensional numerically indexed and associative arrays types. Numerical arrays are referenced using integers, and associative are referenced using strings. Numerically indexed arrays can be accessed from the end using negative indices, the index of -1 references the last element. The indices do not have to be contiguous. Unlike most of the programming languages, Bash array elements don’t have to be of the same data type. You can create an array that contains both strings and numbers. Bash does not support multidimensional arrays, and you can’t have array elements that are also arrays. Creating numerically indexed arrays Bash variables are untyped, any variable can be used as an indexed array without declaring it. To explicitly declare an array, use the declare builtin: declare -a array_name Creating associative arrays Unlike numerically indexed, the associative arrays must be declared before they can be used. To declare an associative array use the declare builtin with the -A (uppercase) option: declare -A array_name Any element can be referenced using the following syntax: ${array_name[index]} The syntax for accessing an array element is similar to the syntax of most of the programming languages. The curly braces ${} are required to avoid shell’s filename expansion operators. The only difference between @ and * is when the form ${my_array[x]} is surrounded with double-quotes. In this case, * expands to a single word where array elements are separated with space. @ expands each array element to a separate word. This is especially important when using the form to illiterate through array elements. To print the keys of the array add the ! operator before the array name: ${!array_name[index]} Here is an example: ## declare the array declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" ) ## print all elements echo "${!my_array[@]}" Output 0 1 2 3 Array Length To get the length of an array, use the following form: ${#array_name[@]} The syntax is the same as when referencing all elements wit addition of the # character before the array name. Loop through the array The most common way to iterate over each item in an array is by using the for loop : declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" ) ## Array Loop for i in "${my_array[@]}" do echo "$i" done Another way to loop through an array is to get the length of the array and use the C style loop: declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" ) # Length of the array length=${#my_array[@]} # Array Loop for (( i=0; i < ${length}; i++ )) do echo $i ${my_array[$i]} done Add a new element To add a new element to a bash array and specify its index use the following form: my_array[index_n]="New Element" Another way of adding a new element to an array without specifying the index is by using the += operator. You can add one or multiple elements: declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" ) ## add new elements my_array+=( Cobalt Nickel ) ## print all elements echo "${my_array[@]}" Outpit Hydrogen Helium Lithium Beryllium Cobalt Nickel Delete an element To delete a single element, you’ll need to know the element index. An element can be removed using the unset command: unset my_array[index] Let’s see an example: declare -a my_array=( "Hydrogen" "Helium" "Lithium" "Beryllium" ) ## remove element unset my_array[2] ## print all elements echo "${my_array[@]}" Output Hydrogen Helium Beryllium # enddoc