I'm trying to dynamically delete elements from an array in bash based on a script argument of the form '123' where each single digit number in the argument is assumed to be an index of the array which should be removed.
#!/bin/bash
# Doesn't delete an element.
ARRAY=(a b c)
while getopts ":a:" opt; do # run e.g. 'thisscript.h -a 0'
case $opt in
a)
echo -n $OPTARG |\
while read -n 1 c; do
unset ARRAY[$c]
done
;;
esac
done
echo ${ARRAY[@]}
# Deletes an element successfully.
ARRAY=(a b c)
unset ARRAY[0]
echo ${ARRAY[@]}
# Deletes an element successfully.
ARRAY=(a b c)
n=0
unset ARRAY[$n]
echo ${ARRAY[@]}
Write this to e.g. tmp.sh file, chmod +x tmp.sh to make executable, then run 'tmp.sh -a 0'.
Why doesn't the first array element deletion method work, and how can I make it work within the 'read -n 1' context?