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 Range: How to iterate over sequences generated on the shell

You can iterate the sequence of numbers in bash by two ways. One is by using seq command and another is by specifying range...
28/12/2020

Bash Alias Tutorial

How to use bash aliases Most of the users like to use shortcuts for running commands. There are many commands in Ubuntu...
28/12/2020

Bash jq command

JSON data are used for various purposes. But JSON data can’t be read easily from JSON file by using bash script like...
29/12/2020
Bài Viết

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

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024