Change string char at index X Change string char at index X unix unix

Change string char at index X


You can achieve this with sed, the stream line editor:

echo $theStr | sed s/./A/5

First you pipe the output of $theStr to sed, which replaces the fifth character with A.


a="............"b="${a:0:4}A${a:5}"echo ${b}

Here is one really good tutorial on string manipulation.


I don't know if it's elegant, or which version of bash you need, but

theStr="${theStr:0:4}A${theStr:5}"

The first part returns first four characters, then the character 'A', and then all the characters starting with the 6th one