I wanted to know if there was a more efficient to do this. I was to setup a conditional for every letter of the alphabet, like so (I am parsing an array):
for i in "${arr[@]}"; do
if [[ $i == A* ]]; then
echo "$i starts with A"
else echo "$i does not start with A"
fi
done
I want to do this A-Z, is there a better way other than duplicating the if statement 26 times?
for i in "${arr[@]}"; do
if [[ $i =~ [A-Za-z].* ]]; then
j=$i
while [[ $j =~ ...* ]]
do
j=${j%?}
done
echo "$i starts with $j"
else echo "$i does not start with a letter"
fi
done
Check for a letter using regex matching in bash, the find the first character for your output.
LETTERS=$(echo {A..Z})
W=Caesar
echo $W doesn\'t start with ${LETTERS/${W:0:1}}
Caesar doesn't start with A B D E F G H I J K L M N O P Q R S T U V W X Y Z