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.

Adblock detected 😱

My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

Remove All Spaces

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Your support makes a big difference:

I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft:

11 comment

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

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.

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.