Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 772
aF. Avatar asked Feb 08 '12 14:02

aF.


1 Answers

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.

like image 128
shellter Avatar answered Nov 15 '22 08:11

shellter