That's an excellent practical application of substring extraction! To extract the first word from a string, you can combine a few Bash features. There isn't one single "first word" function, but we can achieve it by:
- Finding the position of the first space (or a similar delimiter).
- Extracting the substring from the beginning of the string up to that space.
Here's how you can do it, building on what you've learned:
#!/bin/bash
# Define the string (you can use your existing STRING variable)
FULL_STRING="The quick brown fox jumps over the lazy dog"
echo "Original string: '$FULL_STRING'"
# --- Method 1: Using 'expr index' to find the first space ---
# Find the position of the first space (remember expr index is 1-indexed)
# If no space is found, it returns 0.
FIRST_SPACE_POS=$(expr index "$FULL_STRING" " ")
# If a space is found, subtract 1 to get the 0-indexed length of the first word
if [ "$FIRST_SPACE_POS" -gt 0 ]; then
# The length of the first word is FIRST_SPACE_POS - 1
# Example: "The quick" -> space is at 4 (1-indexed), so word length is 3.
FIRST_WORD_LENGTH=$((FIRST_SPACE_POS - 1))
FIRST_WORD=${FULL_STRING:0:$FIRST_WORD_LENGTH}
else
# If no space, the whole string is one word
FIRST_WORD="$FULL_STRING"
fi
echo "First word (Method 1): '$FIRST_WORD'"
# --- Method 2: Using Bash's built-in parameter expansion (non-substring) ---
# This method is often more concise for getting the first "field" or word
# It's similar to how `cut` or `awk` might work with fields.
FIRST_WORD_METHOD_2=$(echo "$FULL_STRING" | cut -d ' ' -f 1)
# OR using Bash array-like behavior with IFS (Internal Field Separator)
# Read the string into an array, splitting by space. The first element is the first word.
read -r -a WORDS_ARRAY <<< "$FULL_STRING"
FIRST_WORD_METHOD_3=${WORDS_ARRAY[0]}
echo "First word (Method 2 - cut): '$FIRST_WORD_METHOD_2'"
echo "First word (Method 3 - array): '$FIRST_WORD_METHOD_3'"
Explanation of Method 1 (using expr index and substring extraction):
FIRST_SPACE_POS=$(expr index "$FULL_STRING" " "):expr index "$FULL_STRING" " "finds the first occurrence of a space inFULL_STRING.- Remember that
expr indexreturns a 1-indexed position. So, for "The quick...", the space after "The" is at position 4.
if [ "$FIRST_SPACE_POS" -gt 0 ]; then ... fi: Checks if a space was found. IfFIRST_SPACE_POSis 0, it means no space was found, so the whole string is one word.FIRST_WORD_LENGTH=$((FIRST_SPACE_POS - 1)): Sinceexpr indexis 1-indexed, the length of the actual first word isFIRST_SPACE_POS - 1. For "The quick...", ifFIRST_SPACE_POSis 4, the length of "The" is 3.FIRST_WORD=${FULL_STRING:0:$FIRST_WORD_LENGTH}: This is the substring extraction you're familiar with.- It starts at position
0(the very beginning of the string). - It extracts
FIRST_WORD_LENGTHcharacters.
- It starts at position
Explanation of Method 2 (cut and read -a)
These are also common and often more idiomatic Bash ways to handle "words" or "fields" in a string:
$(echo "$FULL_STRING" | cut -d ' ' -f 1):echo "$FULL_STRING"prints your string.- The
|(pipe) sends the output tocut. cut -d ' ' -f 1tellscutto use a space (-d ' ') as a delimiter and extract the first field (-f 1). This is very effective for getting the first word.
read -r -a WORDS_ARRAY <<< "$FULL_STRING":<<< "$FULL_STRING"is a "here string," feeding the content ofFULL_STRINGas standard input toread.read -r -a WORDS_ARRAYreads the input.-rprevents backslash escapes from being interpreted.-a WORDS_ARRAYtellsreadto parse the input into an array namedWORDS_ARRAY, splitting by theIFS(Internal Field Separator) variable, which defaults to whitespace (space, tab, newline).
FIRST_WORD_METHOD_3=${WORDS_ARRAY[0]}: Once the string is split into an array, the first word is simply the first element of that array (WORDS_ARRAY[0]).
For practical scripting, Method 2 (using cut or read -a) is often cleaner and more robust for extracting entire words based on delimiters. Method 1 is a good demonstration of combining expr index with the substring syntax.
You can add these examples to your string_operations.sh file to see them in action!