Home > Mobile >  Remove nth character from a string variable in Linux bash shellscript
Remove nth character from a string variable in Linux bash shellscript

Time:07-22

I have seen many methods for removing some characters based on regular string or special match using tr etc. But I didn't find any way to delete a character based on accurate index in a string.

For example: In var="hello world",I want to delete the 5th char 'o' and then var should be like "hell world". The code should apply to all other normal strings. Thanks in advance.

CodePudding user response:

One method is:

n=5
var="hello world"
var=${var:0:n-1}${var:n}
  • Related