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

Bash builtin examples

builtin candidate_builtin arg … in bash allows you to only call bash builtins. That is, even if an external command or...
29/12/2020

Tổng hợp 15 lệnh OpenVZ commands thông dụng

1, Command to list the running VPSs in a node # vzlist Example: # vzlist CTID NPROC STATUS IP_ADDR...
20/09/2021

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
Bài Viết

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

Check proxy trên trang nào chuẩn nhất❓
01/11/2023

Thuê Proxy chạy Google Ads / Cần chú ý gì khi chọn proxy và email chạy G.G ads?
12/10/2023

Thuê proxy 4G ở đâu uy tín, giá rẻ, chất lượng?
05/10/2023

Vì sao cần thuê proxy xoay? Địa chỉ cung cấp proxy xoay uy tín
05/10/2023

Thuê proxy v6 kéo view Youtube ở đâu uy tín, chất lượng?
05/10/2023