王米尔
王米尔

Reputation: 3

Remove nth character from a string variable in Linux bash shellscript

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.

Upvotes: 0

Views: 263

Answers (1)

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10123

One method is:

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

Upvotes: 2

Related Questions