Friday, July 22, 2022

[SOLVED] Remove nth character from a string variable in Linux bash shellscript

Issue

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.


Solution

One method is:

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


Answered By - M. Nejat Aydin
Answer Checked By - Robin (WPSolving Admin)