Contents
How do I use substring in Linux?
Example 1: To Extract till Specific Characters from Starting
- #!/bin/bash.
- #Script to extract first 10 characters of a string.
- echo “String: We welcome you on Javatpoint.”
- str=”We welcome you on Javatpoint.”
- echo “Total characters in a String: ${#str} “
- substr=”${str:0:10}”
- echo “Substring: $substr”
How do I extract a substring in bash?
Using the cut Command
Specifying the character index isn’t the only way to extract a substring. You can also use the -d and -f flags to extract a string by specifying characters to split on. The -d flag lets you specify the delimiter to split on while -f lets you choose which substring of the split to choose.