Bash Script Replace Empty Spaces String

by on August 14, 2006 · 11 comments· LAST UPDATED June 6, 2011

in , ,

How do I remove all spaces from string using shell scripts? I've var="This is a test", and I'd like to remove all spaces.

You can sed stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is 'sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

Remove All Spaces

echo "This     is    a   test" | sed 's/ //g'
var="This     is    a   test"
echo $var | sed 's/ //g'
 

Replace All Spaces With * Symbol

echo "This     is    a   test" | sed 's/ /*/g'
var="This     is    a   test"
echo $var | sed 's/ /*/g'

Replace All Spaces With Bash

Bash shell supports a find and replace via substitution for string manipulation operation. The syntax is as follows:

 
${varName//Pattern/Replacement}
 

Replace all matches of Pattern with Replacement.

 
x="    This    is      a      test   "
echo "${x// /}"
### replace all spaces with * ####
echo "${x// /*}"
 

Sample outputs:

Thisisatest
****This****is******a******test***

Updated for accuracy!

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 11 comments… read them below or add one }

1 Philippe Petrinko November 14, 2010 at 6:06 pm

Hi,

There is also a builtin bash shell function that you could use, which should be simplier, and quicker (it does not use an extra process like calling sed):


mystring="   Imagination   is    more   important   than   knowledge   "
echo "${mystring// /+}" # this will replace every space by a "+"
+++Imagination+++is+++more+++important+++than+++knowledge+++
echo "${mystring// /}" # this will remove every space
Imaginationismoreimportantthanknowledge

Enjoy !

Reply

2 nixCraft November 14, 2010 at 7:48 pm

Many thanks as always :)

Reply

3 Philippe Petrinko November 14, 2010 at 8:18 pm

My pleasure!
Which is reading again and again your useful and friendly WebSite, as always!
— PP

Reply

4 Richard June 6, 2011 at 4:25 am

Hi,

I don’t know if this is relevant to this post but maybe you can help me.

I am looking for a script either web based or desktop application to change the space between words and change the space into hyphen. I got list of words/keyword phrase that I want to put a hyphen which is time consuming is will do it one by one. So maybe a software or script can help me.
Here is the example.

1 Week Weight Loss Diet
1 Weight Loss Pill
1 Weight Loss Pill For Women

I want that to become like this.

1-Week-Weight-Loss-Diet
1-Weight-Loss-Pill
1-Weight-Loss-Pill-For-Women

The list is in noted.

Thanks

richard

Reply

5 Richard June 6, 2011 at 4:27 am

Correction: noted.

The list is in notepad.

Reply

6 Philippe Petrinko June 6, 2011 at 1:55 pm

So, may I know if this topic could be updated to post to introduce the other solutions I gave?
TIA,
— Philippe

Reply

7 nixCraft June 7, 2011 at 12:01 am

Thanks, this info has been updated.

Reply

8 Roni December 19, 2011 at 9:12 am

Hi there,
Thanks for the info, you’ve helped me in a little bash script.
Regards,
Roni.

Reply

9 newbie.my May 22, 2012 at 9:49 am

I am facing some problem regarding my bash script.

Below is my bash code :

#!/bin/bash

cd /root/Msgs/TESTNEW/new

file=myfile.txt

var1=$(awk ‘(NR==30){print $2}’ $file)
var2=$(awk ‘NR>=38 && NR<=39' $file)
var3=${var2// /+}

curl "http:///power_sms/send_sms.php?username=&password=phoneno=$var1&text=$var3

This scripts purposed is for reading in a range of line in particular file(for ex:myfile.txt).Then it will put the content of the file into some variable(var1,var2).
After that,the variable will be called into the curl functions.

The problems start when the content of the file have spacing in every new line.This making the curl not functioning as it do not accept white space character.I have manage to replace the spacing into plus symbols.But whenever there is new line,it will have spacing rather than having plus symbol.

Some of the output is as below:

hi+there.hopefully+you+can+get+this+email+which+are being+send+as+sms.

Can someone help me?Thanks.

Reply

10 James June 29, 2012 at 12:32 am

Hello,

I have a string CLIENT= hostname. How can i use sed to remove the space between the = and h? I like it to be like CLIENT=hostname.

Thanks,
James

Reply

11 Cakdiq May 23, 2013 at 4:40 pm

Thx a lot bro,
This is worked :)

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , ,

Previous Faq:

Next Faq: