I'm trying to do this:
for string in "${STRING[@]}"
do
LASTCHAR="$(echo \"${string: -1}\")"
if [[ "$LASTCHAR" == "!" ]]; then
#do stuff
fi
done
What I want to do is check the last character of the string in an if statement. I've searched just about everywhere and no solution I've come across works.
The most prevalent solution or what I thought should have worked is one of the answers to this question.
How to get the last character of a string in a shell?
That question doesn't seem to be asking about doing this from within a script though, just from command line. So I've tried:
LASTCHAR="$(echo \"${string: -1}\")"
This is the closest I can get. This gives an extremely ugly red bar after the :
in vim, making me think there is a syntax error.
The script still runs though. And it technically gets the last character of the string, but it also adds double quotes around the character. Every other solution I've tried out there utterly fails.