Variable as Bash Array Index

How to use a variable to index ${array[*]} in bash?

* and @ are not considered regular elements in the array. They are not listed when iterating keys, and are not considered when expanding indirectly through index variables.

The bash source code has a function chk_atstar that checks whether [@] or [*] is being used, and you can see that it's done literally and not through any expansion:

else if (valid_array_reference (name, 0))
{
temp1 = mbschr (name, '[');
if (temp1 && temp1[1] == '@' && temp1[2] == ']')
{

If you really want to do this, you can go through variable indirection:

arr=(one two three)
index='*'
var="arr[$index]"
echo "${!var}"

though you may be better off not trying to treat these special array access modes as array elements.

Passing variable as index to bash array

You say NARR is an array, but you are giving it a string value

Perhaps change to this

NARR=($(awk 'NR>1 {print $2}' "$DIR"all.dat))

Bash shell-Accesing String array with index as variable

As an aside, your code has multiple errors and stylistic issues.

i=0
declare -a str_arr

# Notice IFS to use shell to parse line
# Notice -r to disable legacy behavior
while IFS='|' read -r _ post _ dept _
do
# Notice indent of loop body
str_arr[$i]="$dept|$post"
i=$(( i+1 ))
done< emp_info.dat

# Notice quoting
echo "${str_arr[@]}"

In fact, many times you should simply process the values as you read them, inside the loop, instead of accumulate them into an array, in which case maybe you don't need Bash-only features at all; but perhaps this is part of a bigger program where you really do require random access to all the values by numeric index.

awk -F '|' '{ print $4 OFS $2 }' emp_info.dat

Get the index of a value in a Bash array

This will do it:

#!/bin/bash

my_array=(red orange green)
value='green'

for i in "${!my_array[@]}"; do
if [[ "${my_array[$i]}" = "${value}" ]]; then
echo "${i}";
fi
done

Obviously, if you turn this into a function (e.g. get_index() ) - you can make it generic

Bash - arithmetic in array index

Almost there.

arr[(($x+1))]="hi"

Accessing array index variable from bash shell script loop?

You can loop over index using indirect reference syntax (since Bash 3) :

#!/bin/bash

A=('foo' 'bar' 'baz' 'bat')
for i in ${!A[*]}; do # replace ${A[*]} with ${!A[*]}
echo $i
done

For more : How to iterate over associative arrays in Bash

Using a variable for an array element in shell script

The way you're using "$today" is correct, no problem there.

Don't put spaces around the equal sign. It needs to be file=(...) in both cases. Spaces are not allowed in assignments.

You can use Shell Check to catch these kinds of errors. Here's what it prints:

file = (x1_"$today" x2_"$today" x3_"$today" x4_"$today" x5_"$today")
^-- SC2283: Remove spaces around = to assign (or use [ ] to compare, or quote '=' if literal).
^-- SC1036: '(' is invalid here. Did you forget to escape it?
^-- SC1088: Parsing stopped here. Invalid use of parentheses?


Related Topics



Leave a reply



Submit