How to trim string in bash

29/12/2020
Sometimes it requires to remove characters from the starting and end of the string data which is called trimming. There is a built-in function named trim() for trimming in many standard programming languages. Bash has no built-in function to trim string data. But many options are available in bash to remove unwanted characters from string data, such as parameter expansion, sed, awk, xargs, etc. How you can trim string in bash is shown in this tutorial by using different examples.

Example-1: Trim string data using parameter expansion

Space or any character can be trimmed easily from the string data by using bash parameter expansion. The following commands show the uses of parameter expansion for removing space from the starting and end of the string.

# Declare a variable, $myvar with a string data.
$ myVar=" everyone "

# The following command will show the spaces at the starting and end of the
variable, $myVar
$ echo "Hello $myVar"

# The following command will print the output after removing the spaces from
the beginning
of the variable, $myVar
$ echo "Hello ${myVar##*( )}"

#The following command will print the output after removing the spaces from
the ending of the
variable, $myVar
$ echo "${myVar%%*( )} is welcome to our site"

Example-2: Trim string data using `sed` command

`sed` command is another option to remove leading and trailing space or character from the string data. The following commands will remove the spaces from the variable, $myVar using `sed` command.

# Declare a variable, $myVar with a string data
$ myVar="     Web Design Courses    "

# The following command will print the output with the leading and trailing spaces of the
variable,$myVar
$ echo "I want to learn $myVar from this site"

# The following `sed` command will remove the trailing spaces from the variable
$ myVar=`echo $myVar | sed ‘s/ *$//g’`

# Print the output after removing the spaces
$ echo "I want to learn $myVar from this site"

Use sed ‘s/^ *//g’, to remove the leading white spaces.
There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]].

# Declare the variable, $Var with a string value
$ Var=" PHP and MySQL "

# Print the value of $Var before trimming
$ echo "$Var are very popular now."

#Remove the spaces from the variable
$ Var=`echo $Var | sed -e ‘s/^[[:space:]]*//’`

# Print the value of $Var after trimming
$ echo "$Var are very popular now."

Example-3: Trim string data using `awk` command

`awk` command is another way to trim the string value. The following commands uses `awk` command to remove spaces from the starting and ending of the variable, $Input_text.

# Declare a variable with a string data
$ Input_text=" Desiginning Website with CSS3 "
# Print the value of the variable before trimming
$ echo "${Input_text}"
# Print the string after Removing the spaces from the beginning of the variable
$ echo "${Input_text}" | awk ‘{gsub(/^[ t]+/,""); print $0, " JQuery" }’
# Print the string after Removing the spaces from the end of the variable
$ echo "${Input_text}" | awk ‘{gsub(/[ t]+$/,""); print $0, " JQuery" }’
# Print the string after Removing the spaces from the beginning and end of the variable
$ echo "${Input_text}" | awk ‘{gsub(/^[ t]+| [ t]+$/,""); print $0, " JQuery" }’

Example-4: Trim string data using xargs command

`xargs` is another simple command to trim string data.

# Remove the spaces from the string data using `xargv`
$ echo " Bash Scripting Language " | xargs

Conclusion:

This tutorial shows the different ways to trim string data. The string data needs to trim for various reasons. For example, it is better to remove extra spaces from the starting and end of the data before inserting into the database or compare with other value. This tutorial will help the new users to learn trimming options in bash.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Ansible Archive and Unarchive

Ansible is a great tool to automate your configuration management. The benefit of Ansible is that you don’t need to set...
29/12/2020

Bash History Search

We need to type various commands in the shell for various purposes. Sometimes we need to execute same commands multiple...
29/12/2020

Associative array in Bash

An array variable is used to store multiple data with index and the value of each array element is accessed by the corresponding...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng Dẫn Chọn Dịch Vụ Thuê Địa Chỉ IPv4
10/03/2025

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024