Ora

How do you trim a string in Bash?

Published in Bash String Manipulation 6 mins read

To trim a string in Bash, you can efficiently remove unwanted whitespace from the beginning, end, or throughout a string using built-in parameter expansion or powerful external utilities like sed, awk, tr, and xargs. The best method depends on the specific trimming requirements and whether you need to handle leading, trailing, or all whitespace characters.

String trimming is a fundamental task in shell scripting, essential for cleaning user input, sanitizing data, and ensuring script logic works as expected. It typically involves removing spaces, tabs, and newlines.

Methods for Trimming Strings in Bash

Here are several effective ways to trim strings in Bash, ranging from built-in commands to external utilities.

1. Bash Parameter Expansion (Built-in)

Bash's built-in parameter expansion offers the most performant way to trim characters, as it avoids spawning new processes. This method is particularly efficient for removing leading or trailing instances of specific characters, like spaces. To handle arbitrary whitespace (including tabs, newlines) or patterns, you might need to enable the extglob shell option.

To enable extglob (if needed for patterns like *( )):

shopt -s extglob

Removing Leading Whitespace (Spaces)

This removes the longest matching pattern from the beginning of the string.

string="   Hello World   "
trimmed_leading="${string##*( )}" # Requires 'shopt -s extglob' for pattern
echo "$trimmed_leading"
# Output: Hello World   

Removing Trailing Whitespace (Spaces)

This removes the longest matching pattern from the end of the string.

string="   Hello World   "
trimmed_trailing="${string%%*( )}" # Requires 'shopt -s extglob' for pattern
echo "$trimmed_trailing"
# Output:    Hello World

Removing Both Leading and Trailing Whitespace (Spaces)

You can combine the above methods in two steps.

string="   Hello World   "
# First, remove leading spaces
temp="${string##*( )}" # Requires 'shopt -s extglob'
# Then, remove trailing spaces from the result
trimmed="${temp%%*( )}" # Requires 'shopt -s extglob'
echo "$trimmed"
# Output: Hello World

Note: For general whitespace (including tabs, newlines) without relying on extglob for parameter expansion, sed is often a more straightforward and robust solution.

2. Using sed (Stream Editor)

sed is a powerful command-line utility for text transformations using regular expressions. It's excellent for more complex trimming tasks, especially when dealing with various types of whitespace.

Removing Leading and Trailing Whitespace (Combined)

This is a common and robust method using regular expressions to target any whitespace character ([[:space:]]).

string="   Hello World   "
trimmed=$(echo "$string" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
echo "$trimmed"
# Output: Hello World
  • ^[[:space:]]*: Matches zero or more whitespace characters at the beginning of the line.
  • [[:space:]]*$: Matches zero or more whitespace characters at the end of the line.
  • -e: Allows multiple sed expressions to be applied sequentially.

Removing All Whitespace (Global)

To remove every instance of a whitespace character, use the global substitution flag g.

string="   Hello World   "
trimmed=$(echo "$string" | sed 's/[[:space:]]//g')
echo "$trimmed"
# Output: HelloWorld

3. Using tr (Translate or Delete Characters)

The tr command is highly effective for character-by-character translation or deletion. It's particularly useful for removing all occurrences of specific characters.

Removing All Spaces

To remove all space characters from a string:

string="   Hello World   "
trimmed_string=$(echo "$string" | tr -d ' ')
echo "$trimmed_string"
# Output: HelloWorld
  • -d: Stands for "delete characters".
  • ' ': Specifies the space character to be deleted.

Removing All Whitespace (Spaces, Tabs, Newlines)

To delete all common whitespace characters using the [:space:] character class:

string="  Hello\tWorld\n  "
trimmed=$(echo -e "$string" | tr -d '[:space:]')
echo "$trimmed"
# Output: HelloWorld
  • echo -e: Interprets backslash escapes (\t for tab, \n for newline).
  • [:space:]: A character class that includes spaces, tabs, newlines, vertical tabs, form feeds, and carriage returns.

Compressing Multiple Spaces to a Single Space

The -s (squeeze-repeats) option of tr can replace sequences of a repeated character with a single instance.

string="Hello    Beautiful    World"
trimmed=$(echo "$string" | tr -s ' ')
echo "$trimmed"
# Output: Hello Beautiful World

4. Using awk (Pattern Scanning and Processing Language)

awk is a powerful text processing tool with built-in functions for string manipulation, making it suitable for trimming and other data transformations.

Removing Leading and Trailing Whitespace

A direct way to remove both leading and trailing whitespace using awk's gsub function (global substitution):

string="   Hello World   "
trimmed=$(echo "$string" | awk '{gsub(/^[[:space:]]+|[[:space:]]+$/,""); print}')
echo "$trimmed"
# Output: Hello World
  • gsub(/^[[:space:]]+|[[:space:]]+$/,""): Globally substitutes (removes) one or more leading (^[[:space:]]+) or trailing ([[:space:]]+$) whitespace characters.

5. Using xargs

While primarily used for building and executing command lines, xargs can also be used as a simple string trimmer. By default, xargs treats whitespace as delimiters and then outputs its arguments separated by a single space, effectively trimming the input.

Trimming a Single Line

string="   Hello World   "
trimmed=$(echo "$string" | xargs)
echo "$trimmed"
# Output: Hello World

Note: This method also normalizes multiple internal spaces to a single space and removes any quotes around the string.

Summary Table of Trimming Methods

Method Purpose Example (Input: " Hello World ") Output
Bash Parameter Expansion Remove leading/trailing specific characters string=" Hello World "
shopt -s extglob
temp="${string##*( )}"
final="${temp%%*( )}"
Hello World
sed Regex-based trimming of any whitespace echo "$string" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' Hello World
tr -d ' ' Delete all spaces echo "$string" | tr -d ' ' HelloWorld
tr -d '[:space:]' Delete all whitespace characters echo -e " Hello\tWorld " | tr -d '[:space:]' HelloWorld
tr -s ' ' Squeeze multiple spaces to one echo "Hello World" | tr -s ' ' Hello World
awk Advanced string manipulation with regex echo "$string" | awk '{gsub(/^[[:space:]]+|[[:space:]]+$/,""); print}' Hello World
xargs Trim and normalize spaces echo "$string" | xargs Hello World

Practical Insights

  • For pure Bash solutions and speed, use parameter expansion, especially if extglob is enabled for more robust patterns.
  • For robust and general whitespace trimming (leading, trailing, or all types), sed is typically the most versatile and recommended tool due to its powerful regular expression capabilities.
  • When you need to delete specific characters or compress repeated characters globally, tr is highly efficient and straightforward.
  • awk provides powerful text processing and can be integrated into more complex data manipulation workflows where trimming is just one step.
  • xargs offers a quick way to trim and normalize spaces in a single line, but be aware of its side effects (like handling quotes).

Choosing the right method depends on your specific needs, the complexity of the trimming required, and whether you prioritize performance or broader functionality.