Bash – Remove character at specific index in string

To remove character at specific index in a string in Bash scripting, you can use string slicing. Get the substring upto the specified index, and the substring after the specified index, and join these two parts, as shown in the following expression.

</>
Copy
${string::index}${string:index+1}

where

  • index is the specific position from which we would like to remove the character in string.

Example

In the following script, we take a string in str. We remove the character at index=5 and print the resulting string.

example.sh

</>
Copy
#!/bin/bash
 
string="helloworld"
index=5
output=${string::index}${string:index+1}
echo $output

Bash Version: GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)

Output

sh-3.2# bash example.sh 
helloorld

References

Bash Substring

Conclusion

In this Bash Tutorial, we learned how to remove the character at specific index in given string.