Bash – Remove first character in string

To remove first character in a string in Bash scripting, you can use string slicing. Get the substring from second character till the end the string, as shown in the following expression.

</>
Copy
${string:1}

Example

In the following script, we take a string in str. We remove the first character in the string, and print the resulting string using echo.

example.sh

</>
Copy
#!/bin/bash
 
string="helloworld"
index=5
output=${string: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 
elloworld

References

Bash Substring

Conclusion

In this Bash Tutorial, we learned how to remove the first character in given string using string slicing.