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:
- 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
- Using tr command:
tr [option] ' '
- Using grep command:
echo "$input_string" | grep [option] '[regular_expression]'
- Using sed command:
sed 's/ //g'
- Using awk command:
echo "$input_str" | awk 'pattern { action }'
- 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'"
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.
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}"
At first, the script removes the shortest match from the beginning using${var#a*b
} which starts witha
and 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 withb
and ends with c
. At last, the${var%%b*c}
removes the longest match from the end.
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"
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.
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'"
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 theecho
command. The echo command prints the original string and the trimmed string.
From the image, you can see that the parameter expansion has trimmed the unnecessary spaces between the 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"
At first, the script declares a string in a variable that contains spaces, tabs, and newlines. With -d ''
thetr
command deletes the tabs( ), newline(), and spaces( ) from the input string. Finally, theecho
command prints the modified trimmed string in the terminal.
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 thetr
command with the-s
option. 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"
Firstly the script declares the input string which contains leading, trailing, and internal spaces. Then the pipe operator takes the output from theecho
command 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
.
The output shows the script has deleted the leading and trailing spaces and also the spaces that are between the substrings.
-d
option with thetr
command, deletes all the spaces of a string. For example, if you have a string “Hello World”, thetr -d
will return an output like HelloWorld. On the other hand, thetr -s
will 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 grep
command with-o
option. For example, use the script below:
#!/bin/bash
string=$' Bash\nis\nawesome! '
trimmed_string=$(echo "$string" | grep -o '[^[:space:]].*[^[:space:]]')
echo "$trimmed_string"
Firstly, the script declares an input string containing leading and trailing spaces and newline characters (\n). Then the pipe(|) operator redirects the output of theecho
command 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-o
option.
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 thesed
command, 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'"
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.
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 theawk
command:
#!/bin/bash
string=" Hello, World! "
trimmed=$(echo "$string" | awk '{$1=$1};1')
echo "Trimmed String: '$trimmed'"
In the script, the{$1=$1}
sets the first field to the first argument. Then theawkc
ommand reformats the input string which has the default file separator. In the output string, the awk command deletes the trailing and leading space.
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"
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.
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"
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(/, /, ",")
, theawk
command 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
.
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.
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'"
With the $(echo "$string" | xargs)
, the script pipes the output of theecho
command to the xargs command. Without any options and arguments, thexargs
command deletes the leading and trailing whitespaces from the input 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
- How to Convert a Bash String to Int? [8 Methods]
- How to Format a String in Bash? [Methods, Examples]
- How to Convert Bash String to Uppercase? [7 Methods]
- How to Convert Bash String to Lowercase? [7 Methods]
- How to Replace String in Bash? [5 Methods]
- How to Truncate String in Bash? [5 Methods]
- How to Remove Character from String in Bash? [7+ Methods]
- How to Remove First Character From Bash String? [7 Methods]
- How to Remove Last Character from Bash String [6 Methods]
- How to Use String Functions in Bash? [Examples Included]
- How to Generate a Random String in Bash? [8 Methods]
- How to Manipulate Bash String Array [5 Ways]
- How to Use “Here String” in Bash? [Basic to Advance]
- Encode and Decode with “base64” in Bash [6 Examples]
- How to Use “eval” Command in Bash? [8 Practical Examples]
- How to Get the First Character from Bash String? [8 Methods]
<< Go Back to String Manipulation in Bash | Bash String | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners