Bash Script Replace Empty Spaces String

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:


Replace all matches of Pattern with Replacement.

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

Sample outputs:


Updated for accuracy!

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • Philippe Petrinko Nov 14, 2010 @ 18:06


    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 "+"
    echo "${mystring// /}" # this will remove every space

    Enjoy !

  • Philippe Petrinko Nov 14, 2010 @ 20:18

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

  • Richard Jun 6, 2011 @ 4:25


    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.


    The list is in noted.



  • Richard Jun 6, 2011 @ 4:27

    Correction: noted.

    The list is in notepad.

  • Philippe Petrinko Jun 6, 2011 @ 13:55

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

  • Roni Dec 19, 2011 @ 9:12

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

  • May 22, 2012 @ 9:49

    I am facing some problem regarding my bash script.

    Below is my bash code :


    cd /root/Msgs/TESTNEW/new


    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.

  • James Jun 29, 2012 @ 0:32


    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.


  • Cakdiq May 23, 2013 @ 16:40

    Thx a lot bro,
    This is worked :)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @