array in bash shell script code example
Example 1: bash array
# create an array - just assign first value
arrayname[0]='VALUE'
# define an array
declare -a arrayname=(element1 element2 element3)
# get array element at index
${arrayname[2]}
# get all array elements
${arrayname[@]}
# get array length
${#arrayname[@]}
Example 2: bash list of integers
vals=($(seq 0 0.1 2.5))
Example 3: store command into array bash
#Just use following structure to store output of command into a variable:
var=$(command)
#For example:
var=$(echo 'hi') #store hi into var
array=($(ls)) #store list of files into var
Example 4: bash script in array
if [[ " ${array[@]} " =~ " ${value} " ]]; then
# whatever you want to do when array contains value
fi
if [[ ! " ${array[@]} " =~ " ${value} " ]]; then
# whatever you want to do when array doesn't contain value
fi