0

I want to identify last character of string, I am accessing the last character using:

echo "${VAR: -1}"

If it's a character, I want to add a .(dot) before the character and do nothing if it's a number.

1 Answer 1

1
expr match ${VAR: -1} '.*[0-9]' >/dev/null || VAR="${VAR:0:`expr ${#VAR} - 1`}.${VAR: -1}"

explanation:

expr match ${VAR: -1} '.*[0-9]' >/dev/null    

The first expression returns 1 if last character is a number

VAR="${VAR:0:`expr ${#VAR} - 1`}.${VAR: -1}"

If the expression returns 1 then the rest of the line assigns var to all chars in var up to but not including the last char, appends a "." character, and appends the last character.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.