1. Introduction

In this tutorial, we’ll learn how to extract the first word of a string in Bash.

We’ll use several methods to explore this, including Bash parameter expansion, awk, sed, cut, grep, and expr commands. However, some of these commands are more complex than others. Ultimately, we can decide which method is more suitable for our task.

These commands have been tested on CentOS 9 and Ubuntu 22.04 LTS but can also be applied to other Linux distributions.

2. Using Parameter Expansion

Parameter expansion is a built-in function of Bash. It allows us to manipulate variable values.

We can efficiently use it to extract the first word from a string:

$ var="Welcome to Baeldung"
$ first_word="${var%% *}"
$ echo "The first word of the string is: $first_word"

Let’s understand the command:

  • The string has been stored inside the variable var
  • “${var%% *}” is used as a parameter expansion to store only the first word in a new variable first_word, up to the first space
  • The echo command displays the value of the first_word on the terminal

So, when we run these commands, we’ll get the following results:

The first word of the string is: Welcome

The output shows the first word of the input string.

With each of the commands we discuss, we can use any other string instead of “Welcome to Baeldung” and search for its first word. However, we’ll use the same input string throughout this tutorial, with the expected output as “Welcome”.

3. Using the awk Command

We mainly use the awk command for text processing that involves data manipulation and calculations.

We can also use it to extract the first word from a string:

$ echo "Welcome to Baeldung" | awk '{print $1}'

Let’s break down this command:

  • The echo command prints the string to the standard output
  • The pipe (|) operator takes the output string and provides it as an input to awk
  • ‘{print $1}’ instructs awk to print the first word of the provided string

This command splits the words of the input string at each space character and displays the first word.

4. Using the sed Command

sed is a stream editor focusing on pattern matching and text editing.

We can use the following code to extract the first word of a string:

$ echo "Welcome to Baeldung" | sed 's/ .*//'

Here, we use s/ .*//’  for text substitution, i.e., to replace the text matching the given regular expression with the empty string.

When we execute this command, it trims the text by replacing any words after the first word with the empty string and then prints the result.

5. Using the cut Command

We mainly use the cut command to display a specific part of some text, such as the first field in a row delimited by a particular character.

Let’s use it to extract the first word in our input string:

$ echo "Welcome to Baeldung" | cut -d " " -f1

Here, the option cut -d  ” “ tells the cut command to use the space character as a delimiter. We use -f1 to display only the first field.

This command extracts the first space delimited word from the string.

6. Using the grep Command

The grep command can search for specific text patterns, like words, codes, or phrases. It’s a handy command, especially when dealing with large text, as it can quickly pinpoint the desired information.

In addition to its searching capabilities, we can use it to extract the first word of a string:

$ echo "Welcome to Baeldung" | grep -o '^[^ ]*'

Let’s analyze this command:

  • The -o flag tells grep to output only the matched part
  • ^ represents the start of the line
  • [^ ]* matches characters until the first space is found

When we execute this command, it includes characters from the beginning of the line until reaching the first space.

7. Using the expr Command

The expr command performs different mathematical calculations, but we can also use it to manipulate strings:

$ var="Welcome to Baeldung"
$ first_word=$(expr "$var" : '\([^[:space:]]\+\)')  
$ echo "$first_word"

Let’s see how this command works:

  • The expr command is used here for text manipulation
  • : ‘\([^[:space:]]\+\)’) uses a regular expression to specify the pattern for capturing the first sequence of non-space characters in the input string

8. Conclusion

In this article, we’ve explored various methods to extract the first word of a string.

First, we discussed the parameter expansion method. Next, we explored commands like awk, sed, cut, grep, and expr. These methods can effectively extract the first word from a string.

Parameter expansion is a built-in feature in many shells and is suitable for handling simple and practical tasks. We can efficiently use it without relying on any external commands. The cut command is also a great choice, as its primary function is to extract a field from a character delimited row. The rest of the commands can also effectively do this job, but they are more suited for handling complex tasks due to their broader capabilities.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.