How can I remove the last character of a string variable in ksh?

20,709
var="testing1"
print ${var%?}

output

testing

The ${var%?} is a parameter editing feature. The '%' says remove from the right side and expects a pattern following. The pattern could be in your example case just the char '1' (without the quotes). I am using the wild-card char '?' so that any single character will be removed. You can use the '*' char to indicate all chars, but typically you want to 'bundle' that with some preceding chars, with your example echo ${var%i*} would give you just test as a result. There are also '%%' variants on this AND '#' and '##' that start from the left side of the string.

I hope this helps.

Share:
20,709
aF.
Author by

aF.

Last time I checked,                 I was me.

Updated on February 09, 2020

Comments

  • aF.
    aF. about 4 years

    I've a string variable and I want to remove the last character of it.

    For example: pass from "testing1" to "testing".

    How can I do this in KSH?