How to extract the first word?

QuestionsQuestions4 SkillsProDec, 13 2025
0356

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:

  1. Finding the position of the first space (or a similar delimiter).
  2. 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):

  1. FIRST_SPACE_POS=$(expr index "$FULL_STRING" " "):
    • expr index "$FULL_STRING" " " finds the first occurrence of a space in FULL_STRING.
    • Remember that expr index returns a 1-indexed position. So, for "The quick...", the space after "The" is at position 4.
  2. if [ "$FIRST_SPACE_POS" -gt 0 ]; then ... fi: Checks if a space was found. If FIRST_SPACE_POS is 0, it means no space was found, so the whole string is one word.
  3. FIRST_WORD_LENGTH=$((FIRST_SPACE_POS - 1)): Since expr index is 1-indexed, the length of the actual first word is FIRST_SPACE_POS - 1. For "The quick...", if FIRST_SPACE_POS is 4, the length of "The" is 3.
  4. 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_LENGTH characters.

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 to cut.
    • cut -d ' ' -f 1 tells cut to 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 of FULL_STRING as standard input to read.
    • read -r -a WORDS_ARRAY reads the input.
      • -r prevents backslash escapes from being interpreted.
      • -a WORDS_ARRAY tells read to parse the input into an array named WORDS_ARRAY, splitting by the IFS (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!

0 Comments

no data
Be the first to share your comment!