Checking the last character of a string
Is there a built-in POSIX equivalent for this bashism?
my_string="Here is a string"
last_character=${my_string: -1}
I keep seeing things like this recommended, but they seem like hacks.
last_character=$(echo -n "$my_string" | tail -c 1)
last_character=$(echo -n "$my_string" | grep -o ".$")
But maybe a hack is all we have with POSIX wrappers?
+3
source to share
3 answers