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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With