First, there's no need to invent a dummy index - you can access the array's indices using the indirection operator !
Second, "${Input_Arr[$var]}"
is the element's value; unset
needs the element's name, Input_Arr[$var]
or just Input_Arr[var]
, since it's already an arithmetic context). So given:
$ arr=(foo '' bar '' baz) $ declare -p arr declare -a arr=([0]="foo" [1]="" [2]="bar" [3]="" [4]="baz")
then
$ for i in ${!arr[@]}; do [[ -z ${arr[i]} ]] && unset arr[i]; done
leaves
$ declare -p arr declare -a arr=([0]="foo" [2]="bar" [4]="baz")
This also works for associative arrays - with suitable adjustments for the non-numeric keys (including double quoting expansions to prevent potential split + glob):
$ declare -A Arr=(['1st val']=foo ['2nd val']='' ['3rd val']=bar ['4th val']='' ['5th val']=baz) $ declare -p Arr declare -A Arr=(["5th val"]="baz" ["2nd val"]="" ["4th val"]="" ["3rd val"]="bar" ["1st val"]="foo" ) $ for i in "${!Arr[@]}"; do [[ -z ${Arr[$i]} ]] && unset Arr["$i"]; done $ declare -p Arr declare -A Arr=(["5th val"]="baz" ["3rd val"]="bar" ["1st val"]="foo" )