Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
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.
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:
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”.
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:
This command splits the words of the input string at each space character and displays the first word.
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.
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.
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:
When we execute this command, it includes characters from the beginning of the line until reaching the first space.
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:
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.