shell get the last character of a string

I have wrote the following lines to obtain the last character of a string:

It works for “abcd/”:

It does not work for “abcd*”:

It lists the files in the current folder.

That’s one of the reasons why you need to quote your variables:

Otherwise, bash expands the variable & in this case does globbing before printing out. It is moreover better to quote the parameter to the script (in case you have a matching filename):

Also see the order of expansions in the bash reference manual. Variables are expanded before the filename expansion.