Bash Script Replace Empty Spaces String

Posted on in Categories , , , , , , , , , last updated June 6, 2011

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!

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 11 comments/add one below):

11 comment

  1. 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 !

  2. 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

  3. 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.

Leave a Comment