How to Trim String in Bash? [6 Methods]

In Bash scripting, trimming is the process of removing the whitespaces from the beginning and end of a string. Here, whitespace characters include spaces, tabs, and newlines.

To trim whitespaces from a string in Bash, check the following methods:

  1. Using the parameter expansion:
    • ${parameter##pattern} # Remove longest prefix matching pattern
    • ${parameter#pattern} # Remove shortest prefix matching pattern
    • ${parameter%%pattern} # Remove longest suffix matching pattern
    • ${parameter%pattern} # Remove shortest suffix matching pattern
  2. Using tr command: tr [option] ' '
  3. Using grep command: echo "$input_string" | grep [option] '[regular_expression]'
  4. Using sed command: sed 's/ //g'
  5. Using awk command: echo "$input_str" | awk 'pattern { action }'
  6. Using xargs command: echo $input_string | xargs

Dive into the article to learn these methods of how to trim white spaces from a Bash string in detail.

1. Using Parameter Expansion

To trim the leading and trailing white spaces from the input string, you can use the parameter expansion. Parameter expansion is a pure bash scripting method that does not depend on the external command. So, using the pattern-matching parameter expansion, you can simply remove the leading and trailing spaces. Check the following script:

#!/bin/bash

# Define a sample string
string="   Hello, world!   "

# Trim leading whitespace
trimmed_string="${string#"${string%%[![:space:]]*}"}"

# Trim trailing whitespace
trimmed_string="${trimmed_string%"${trimmed_string##*[![:space:]]}"}"

echo "Original string: '$string'"
echo "Trimmed string: '$trimmed_string'"
EXPLANATION

Firstly, the script declares a string in a variable named string, and the string contains leading and trailing spaces. In ${string#"${string%%[![:space:]]*}"}, the string# removes the prefix in the following double quotes. Then the ![:space:] matches any character except for space and the * matches multiple occurrences of the preceding pattern. At last, the script removes the leading spaces from the string. In the same process, {trimmed_string%"${trimmed_string##*[![:space:]]}"} removes the trailing spaces.

Trim using parameter expansion

The output shows the script successfully trims the leading and trailing spaces from the input string.

Trim Specific Characters

To trim specific characters from the string using parameter expansion, use the following script:

#!/bin/bash

# Define the variable
var="ababc"

# Remove shortest match of 'a*b' from the beginning
echo "Original variable: $var"
echo "Remove shortest match of 'a*b' from the beginning: ${var#a*b}"

# Remove the longest match of 'a*b' from the beginning
echo "Original variable: $var"
echo "Remove longest match of 'a*b' from the beginning: ${var##a*b}"

# Remove shortest match of 'b*c' from the end
echo "Original variable: $var"
echo "Remove shortest match of 'b*c' from the end: ${var%b*c}"

# Remove longest match of 'b*c' from the end
echo "Original variable: $var"
echo "Remove longest match of 'b*c' from the end: ${var%%b*c}"
EXPLANATION

At first, the script removes the shortest match from the beginning using${var#a*b} which starts withaand ends withb. Then it finds and removes the longest match using${var##a*b}. After that, the${var%b*c}finds and removes the shortest match from the end that starts withband ends with c. At last, the${var%%b*c}removes the longest match from the end.

Trim characters using parameter expansion

The output shows that at first the script has removed a short string that starts with a and ends with b and the modified string is abc. Then It has removed the longest characters from the beginning which is abab. After that, it has removed the shortest string from the end which starts with b and ends with c. and the modified string is aba. At last, it has removed the longest string from the end which is babc, and printed the output a.

Using Function with Parameter Expansion

To trim the white spaces you can use a function. The function takes the input and trims all the unnecessary spaces from the input string. Take a look at the following script:

#!/bin/bash

trim_string() {
# Usage: trim_string " example string "
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}

# Example usage:
example_string="   This is an example string with whitespace.         "

echo "Original string:"
echo "$example_string"
echo "Trimmed string:"

trim_string "$example_string"
EXPLANATION

At first, the script declares a function named trim_string. The function removes the leading spaces using the{1#"${1%%[![:space:]]*}"}. Firstly, the![:space:]matches a first nonspace character and then removes all the leading spaces from the argument. The second part of the function_%"${_##*[![:space:]]}removes the trailing spaces from the_variable. Here the_ variable holds the previous part of the string which contains the modified string. At last, the script calls the function with the input argument example_string.

Trim white spaces using parameter expansion

The script shows that the function removes the leading spaces of the input string.

Trim New Line from Input String

To trim the spaces that are between two consequent lines you can use the parameter expansion ${string//$'\n'/}. Check the following script:

#!/bin/bash

# Define a string with newlines
string="Hello,
World!"

# Trim newlines using parameter expansion
trimmed_string="${string//$'\n'/}"

echo "Original String: '$string'"
echo "Trimmed String: '$trimmed_string'"
EXPLANATION

At first, the script declares a two-line string in a variable namedstring. Then with the parameter expansion,${string//$'\n'/}, the script replaces the newline character with spaces. The script uses the//for global pattern substitution in the parameter expansion. To indicate the boundary, it uses the single quotes in theechocommand. The echo command prints the original string and the trimmed string.

Trim line using parameter expansion

From the image, you can see that the parameter expansion has trimmed the unnecessary spaces between the substrings.

Note: This example only trims the newline character. It does not trim the leading, trailing, and extra middle spaces between substrings.

2. Using the “tr” Command

In bash scripting, the tr command is primarily used to translate and delete characters. But you can also use it to trim characters like spaces.

Trim Whitespaces, Tabs, and Newlines

To trim whitespaces, tabs, and newlines from a Bash string using the tr command, check the following script to trim the string:

#!/bin/bash

string_with_newlines_tabs_spaces=$'   Hello,
	World!   '

# Trim newlines, tabs, and spaces
trimmed_string=$(echo "$string_with_newlines_tabs_spaces" | tr -d '')

echo "Original string:"
echo "$string_with_newlines_tabs_spaces"
echo "Trimmed string:"
echo "$trimmed_string"
EXPLANATION

At first, the script declares a string in a variable that contains spaces, tabs, and newlines. With -d '' thetrcommand deletes the tabs( ), newline(), and spaces( ) from the input string. Finally, theechocommand prints the modified trimmed string in the terminal.

Using tr command trim tab, newline and space

The image shows that the script has removed all the spaces, tabs, and newlines from the input string.

Trim Spaces from a Multiline String

To trim the spaces from a multiline string, you can use thetrcommand with the-soption. Check the following script:

#!/bin/bash

# Define a multiline string with leading, trailing, and excess internal spaces
multiline_string="   The first line String,
The Second line String.   "

# Trim spaces using tr command
trimmed_string=$(echo "$multiline_string" | tr -s '[:space:]' ' ')

echo "Original Multiline String:"
echo "$multiline_string"
echo "Trimmed Multiline String:"
echo "$trimmed_string"
EXPLANATION

Firstly the script declares the input string which contains leading, trailing, and internal spaces. Then the pipe operator takes the output from theechocommand to the tr command. After that, the tr command translates all the occurrences of the spaces into a single space. The ‘-s’ option in the tr command, squeezes repeated characters into one. The tr command replaces the[:space:]with nothing. And the script stores modified output in thetrimmed_string.

Trim space from a multiline string

The output shows the script has deleted the leading and trailing spaces and also the spaces that are between the substrings.

Note: The-doption with thetrcommand, deletes all the spaces of a string. For example, if you have a string “Hello World”, thetr -dwill return an output like HelloWorld. On the other hand, thetr -swill squeeze all consecutive spaces into a single space and return an output like Hello World.

3. Using the “grep” Command

The grep command searches and matches the pattern of characters of a string and file. To trim spaces from a Bash string, use grepcommand with-ooption. For example, use the script below:

#!/bin/bash

string=$'   Bash\nis\nawesome!   '

trimmed_string=$(echo "$string" | grep -o '[^[:space:]].*[^[:space:]]')

echo "$trimmed_string"
EXPLANATION

Firstly, the script declares an input string containing leading and trailing spaces and newline characters (\n). Then the pipe(|) operator redirects the output of theechocommand to the grep command. The grep command matches the first non-space character using the regular expression. Then it matches the rest of the characters using .* and at last, it matches the last non-space character. Lastly, the grep command outputs the matched string from the input string using the-ooption.

Trim string in bash using grep command

The output shows that the script has trimmed all the unnecessary spaces from the input string.

4. Using “sed” Command

The sed command is a simple yet very useful command to remove characters, replace strings, match patterns, and manipulate files and texts. Using thesedcommand, you can easily trim all occurrences of the white spaces from the input string. Take a look at the following script:

#!/bin/bash

string="   Hello, World!   "

trimmed=$(echo "$string" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')

echo "Trimmed String: '$trimmed'"
EXPLANATION

In the echo"$string" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//', the sed command removes the leading space using the-e 's/^[[:space:]]*//'. Then thes/[[:space:]]*$//removes the trailing characters. It replaces all the space with nothing which means it effectively removes all the characters.

Trim string in bash using sed command

From the image, you can see that the sed command has deleted all the spaces from the input string.

5. Using “awk” Command

To trim the spaces from an input string, you can use the awk command. It is a versatile command tool used for text processing, manipulation, matching patterns, and removing characters. The awk command simply takes the input and squeezes out the extra spaces between words and trims the extra spaces.

Here’s the script to trim the spaces from a bash string using theawkcommand:

#!/bin/bash

string="   Hello,           World!   "

trimmed=$(echo "$string" | awk '{$1=$1};1')

echo "Trimmed String: '$trimmed'"
EXPLANATION

In the script, the{$1=$1}sets the first field to the first argument. Then theawkcommand reformats the input string which has the default file separator. In the output string, the awk command deletes the trailing and leading space.

Remove all the spaces using awk command

The output shows the awk command has trimmed the unnecessary spaces from the input string.

Trim Multiline String

To trim the spaces from the input of a multiline string use the following script below:

#!/bin/bash

# Define a multiline string with leading, trailing, and excess internal spaces
multiline_string="   The first line String,
The Second line String.   "

# Trim whitespace from multiline string using awk
trimmed_multiline_string=$(echo "$multiline_string" | awk '{$1=$1};1')

echo "Original multiline string:"
echo "$multiline_string"
echo "Trimmed multiline string:"
echo "$trimmed_multiline_string"
EXPLANATION

With the'{$1=$1};1'the awk command reformats the input string that is in the first argument. While reformatting the input string, it uses the default field separator and deletes the leading and trailing spaces.

Trim spaces from a multiline string

The output shows that the script has trimmed the spaces between multi-lines of the input string.

Trim Input File in Bash

To trim the unnecessary whitespaces of an input file in bash, you can use the awk command. Check the following script:

#!/bin/bash

# Input file
input_file="colors.txt"

# Use awk to remove unexpected spaces and tabs, preserving commas
awk '{gsub(/^[[:space:]]+|[[:space:]]+$/, ""); gsub(/[[:space:]]+/, " "); gsub(/, /, ",")} 1' "$input_file" > temp && mv temp "$input_file"
EXPLANATION

Firstly thegsub(/^[[:space:]]+|[[:space:]]+$/, "")globally substitutes the space with an empty string. Then thegsub(/[[:space:]]+/, " ")globally substitutes the space and tabs between two consequent substrings with a single space(” “). After that withgsub(/, /, ","), theawkcommand replaces all the occurrences of “, ” with “,”. The script redirects the modified file to a temporary file namedtemp. At last, the script renames the temp file into theoriginal file name.

Display the contents of the input file

To display the content of the input file, use the cat command following with the filename just like below:

cat colors.txt

From the image, you can see all the contents of the input file which contains multiple unnecessary spaces.

Modify the content of the input file

The script has trimmed all the spaces from the input file colors.txt.

6. Using “xargs” Command

Using the xargs command you can trim the extra spaces from the input string. The xargs command builds and executes command lines from the standard output. However, with this command, you can not trim spaces directly in Bash rather you need to utilize the command by combining it with other commands. Take a look at the following script:

#!/bin/bash

string="   Hello, World!   "
trimmed=$(echo "$string" | xargs)

echo "Trimmed String: '$trimmed'"
EXPLANATION

With the $(echo "$string" | xargs), the script pipes the output of theechocommand to the xargs command. Without any options and arguments, thexargscommand deletes the leading and trailing whitespaces from the input string.

Using xargs command trim spaces from bash string

The image shows that the xargs command has trimmed the extra spaces from the input string.

Conclusion

The article gives an overview of the methods and examples to trim a Bash string in detail. Among all of the methods, the parameter expansion is the pure Bash scripting method. There are other external commands such as tr, grep, and sed which you can use to trim a string. Also, to efficiently trim spaces in Bash script, you can use the handy tool called awk.

People Also Ask

What is trim string in Bash?

Trimming is the process of removing the whitespaces from the beginning and end of a string. Whitespace characters include spaces, tabs, and newlines. Trimming extra spaces makes a string consistent and free from unnecessary padding and helps to handle and compare the strings.

How to trim bash string?

To trim the spaces from a bash string, you can use an external command such as the awk command. Using the awk command you can trim whitespace both from single string and multiline string. If you want to use pure Bash scripting then you can also able to use parameter expansion. Apart from these, there are some other external commands such as sed, tr, and grep that trim the white spaces of a string.

How do I remove a leading new line in Bash?

To remove a leading newline in Bash, you can use the regular expression-based parameter expansion, you can remove the leading and trailing spaces from an input string. It uses #, ##, %, and %% operators for trimming the leading and trailing characters respectively. For example, an input string:

string=”

This is a string with a leading newline.”

To trim the leading new line from the string, use trimmed_string="${string#"${string%%[![:space:]]*}"}".

The command will remove the leading newline and only print “This is a string with a leading newline.”

How do I remove a trailing new line in Bash?

To remove a trailing new line in Bash you can use the sed command. For example, an input string:

input="Hello, World

"

To trim the trailing newline from the string, use input=$(echo -n "$input" | sed -e 's/\n$//'). The command will remove the trailing new line and only print “Hello World”.

Related Articles


<< Go Back to String Manipulation in Bash | Bash String | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Afia Zahin Oishi

Assalamualaikum, I am Afia Zahin, completed my graduation in Biomedical Engineering from Bangladesh University of Engineering and Technology, currently working as a Linux Content Developer Executive at SOFTEKO. A high achieving professional with a strong work ethic and able to work in a team in order to consistently achieve my goal and build my skillset. Able to handle difficult problems with patience and swift decision-making. Read Full Bio

Leave a Comment