Substring Extraction
In this page:
Substring Extraction
${variable:start:length} extracts a portion of a string starting at a given index (0-based) for a given number of characters. Omitting length extracts everything from start to the end. This is Bash's built-in equivalent of a substring function found in other languages.
Note: A negative start, like ${var: -3}, extracts from the end of the string (note the required space before the minus).
Example: Substring Extraction
#!/bin/bash
text="Hello, Bash World!"
echo "${text:0:5}"
echo "${text:7:4}"
echo "${text:7}"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: