Bash Shell: Remove (Trim) White Spaces From String / Variable

by on January 15, 2013 · 9 comments· LAST UPDATED January 18, 2013

in , ,

I have a bash variable as follows:

output="$(awk -F',' '/Name/ {print $9}' input.file)"

How do trim leading and trailing whitespace from bash variable called $output? How do I trim trailing whitespace from $output?

You can use sed, awk, cut, tr, and other utilities to remove whitespace from $output.

Sample data

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsLinux/Unix+Bash/Ksh
Estimated completion timeN/A

Define a variable called ouput:

output=" This is a test"

Use echo statement to display $output:

echo "=${output}="
Sample outputs:

=    This is a test=

sed Example

The syntax is:

 
echo "${output}" | sed -e 's/^[ \t]*//'
 

Sample outputs:

This is a test

Bash Example

The syntax is to remove leading whitespaces:

${var##*( )}

For example:

# Just remove leading whiltespace
#turn it on
shopt -s extglob
 
output="    This is a test"
output="${output##*( )}"
echo "=${output}="
 
# turn it off
shopt -u extglob
 

Sample outputs:

=This is a test=

To trim leading and trailing whitespace using bash, try:

#turn it on
shopt -s extglob
output="    This is a test    "
 
### Trim leading whitespaces ###
output="${output##*( )}"
 
### trim trailing whitespaces  ##
output="${output%%*( )}
echo "=${output}="
 
# turn it off
shopt -u extglob

Sample outputs:

=This is a test=

awk Example

The syntax is:

 
output="    This is a test    "
echo "=${output}="
 
## Use awk to trim leading and trailing whitespace
echo "${output}" | awk '{gsub(/^ +| +$/,"")} {print "=" $0 "="}'
 

Sample outputs:

=This is a test=
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 9 comments… read them below or add one }

1 xof January 18, 2013 at 8:43 am

check the title it says “While” Spaces

Reply

2 nixCraft January 18, 2013 at 9:28 am

Thanks for the heads up!

Reply

3 Eugéne January 23, 2013 at 12:37 pm

I tend to just stick to sed for this typeof thing:

jsoap@nixbox:~$ output=" This is a test  "
jsoap@nixbox:~$ echo "=${output}="
= This is a test  =
jsoap@nixbox:~$ output=$( echo "${output}"| sed -e 's/^ *//' -e 's/ *$//' );
jsoap@nixbox:~$ echo "=${output}="
=This is a test=
jsoap@nixbox:~$

Since a lot of my work needs to live on BSD, AIX and Solaris, as well as on Linux, I tend to script very defensively and portably. It has saved my butt more times than bears mentioning…

Reply

4 Shantanu Gadgil February 16, 2013 at 10:34 am

If the content is a single line of text, why not just use echo ?

$VAR=$(echo $VAR)

notice there are no double-quotes for the inner $VAR

Reply

5 Shantanu Gadgil February 21, 2013 at 9:26 am

… slight error …. it should be

VAR=$(echo $VAR)

Reply

6 Keith February 22, 2013 at 6:22 pm

This is the BEST site in the world. I can’t tell you how many times I have wound up here to find the answer to my question or problem. Great resource, thanks!!!

Reply

7 Krishna Chappidi February 26, 2013 at 3:41 pm

Hello..
Could you please help me out….
Here is my scenario:
1. i am listing some of the processes which are active and sending the output to temp.txt.
2. i got many processes, if i want to kill/doing some thing with any inactive process it should throw an error message.
3. how do i work with pattern matching commands in temp.txt?
Please suggest me the better answer..

Reply

8 Larry Helms February 27, 2013 at 1:50 am
m=$( echo "${output}" | sed -e "s/^\ *//g" -e "s/\ *$//g")
echo "=${m}="
=This is a test=

Reply

9 Leszek February 4, 2014 at 8:11 am

Very helpful. Thanks.

Reply

Leave a Comment

Tagged as: , ,

Previous Faq:

Next Faq: