Chapter 14: Extracting Substrings

Using Bash Substring Expansion

  • Syntax: ${str_var:offset:length}
  • Start extracting from the offset, up to the specified length.
  • The offset is used to specify the position from where to start the extraction of a string. The length is used to specify the range of characters to be extracted, excluding the offset
  • Note: Indexing is 0-based
1 # Example
2 u="Hello World"
3 # Start from index 0 and extract upto length 5
4 echo ${u:0:5} # output: Hello

Using IFS

IFS (Internal Field Separator), is an environment variable that determines how Bash recognizes word boundaries while splitting a sequence of character strings. Default values are space, tab and newline.

In the below example, we have set the IFS to an underscore, which is what will be used to split the numbers variable with underscore as the delimiter. Once split, we can access the words using $1, $2 and so on.

1 # Example 1
2 
3 numbers=one_two_three_four_five
4 IFS="_"
5 
6 # Setting positional parameters $1, $2 and so on
7 set $numbers
8 
9 echo $2 #output will be two
 1 # Example 2: using space as IFS
 2 
 3 greeting="Hello World"
 4 IFS=" "
 5 
 6 # Save the split string in an array
 7 read -a array_var <<< "$greeting"
 8 
 9 echo "There are ${#array_var[@]} words in the string"
10 
11 for word in ${array_var[@]}:
12 	do
13 			printf "$word\n"
14 done

Using cut

  • -d option allows us to specify the delimiter
  • -f sets the field to be extracted
1 numbers="one_two_three_four_five"
2 substring=$(echo $numbers | cut -d "_" -f 3)
3 echo $substring # will output three