What does the syntax ${STRING:$START:$LENGTH} mean?

QuestionsQuestions8 SkillsProBasic String OperationsSep, 10 2025
0127

The syntax ${STRING:$START:$LENGTH} is used in Bash to extract a substring from a string. Here's what each part means:

  • STRING: This is the variable containing the original string from which you want to extract a substring.

  • $START: This is the starting position (0-indexed) from where the extraction begins. For example, if START=10, it starts extracting from the 11th character of the string.

  • $LENGTH: This specifies how many characters to extract from the starting position. For instance, if LENGTH=5, it will extract 5 characters starting from the position defined by START.

Example:

If you have:

STRING="The quick brown fox"
START=10
LENGTH=5
SUBSTRING=${STRING:$START:$LENGTH}
  • START=10 points to the character 'b' in "brown".
  • LENGTH=5 extracts "brown".

So, SUBSTRING will contain "brown".

If you have more questions or need further examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!