20

I have done the following at command line:

$ text="name with space"
$ echo $text
name with space

I am trying to use tr -d ' ' to remove the spaces and have a result of:

namewithspace

I've tried a few things like:

text=echo $text | tr -d ' '

No luck so far so hopefully you wonderful folk can help!

drs
  • 5,363
  • 9
  • 40
  • 69
user3347022
  • 465
  • 2
  • 6
  • 14

5 Answers5

52

In Bash, you can use Bash's built in string manipulation. In this case, you can do:

> text="some text with spaces"
> echo "${text// /}"
sometextwithspaces

For more on the string manipulation operators, see http://tldp.org/LDP/abs/html/string-manipulation.html

However, your original strategy would also work, your syntax is just a bit off:

> text2=$(echo $text | tr -d ' ')
> echo $text2
sometextwithspaces
Steven D
  • 45,310
  • 13
  • 119
  • 114
11

You don't need echo command at all, just use Here String instead:

text=$(tr -d ' ' <<< "$text")

Just for curiosity I checked how much time such a trivial task takes for different tools. Here are the results sorted from slowest to fastest:

abc="some text with spaces"

$ time (for i in {1..1000}; do def=$(echo $abc | tr -d ' '); done)
0.76s user 1.85s system 52% cpu 4.976 total

$ time (for i in {1..1000}; do def=$(awk 'gsub(" ","")' <<< $abc); done)
1.09s user 2.69s system 88% cpu 4.255 total

$ time (for i in {1..1000}; do def=$(awk '$1=$1' OFS="" <<< $abc); done)
1.02s user 1.75s system 69% cpu 3.968 total

$ time (for i in {1..1000}; do def=$(sed 's/ //g' <<< $abc); done)
0.85s user 1.95s system 76% cpu 3.678 total

$ time (for i in {1..1000}; do def=$(tr -d ' ' <<< $abc); done)
0.73s user 2.04s system 85% cpu 3.244 total

$ time (for i in {1..1000}; do def=${abc// /}; done)
0.03s user 0.00s system 59% cpu 0.046 total

The pure shell operation is definitely the fastest what is not surprising, but what is really impressive that it is over 100 times faster then the slowest command!

jimmij
  • 46,064
  • 19
  • 123
  • 136
7

Just modify your text variable as below.

text=$(echo $text | tr -d ' ')

However, if we have control characters this might break. So, as per Kasperd's suggestion, we could have double quotes around it. So,

text="$(echo "$text" | tr -d ' ')"

will be a better version.

Ramesh
  • 38,687
  • 43
  • 140
  • 215
  • Wonderful! I was soooo close. As a noob I am pleased I was heading the right direction! Thank you for the fast response as well, as soon as I have waited 8 minutes I will submit this as the answer! – user3347022 Sep 20 '14 at 16:38
  • @user3347022, you are welcome :) – Ramesh Sep 20 '14 at 16:52
  • 1
    This is going to break if `$text` contains control characters, which would be interpreted by the shell. Better put some double-quotes in there: `text="$(echo "$text" | tr -d ' ')"` – kasperd Sep 21 '14 at 22:01
  • @kasperd, thanks for mentioning it. I have incorporated your suggestion. – Ramesh Sep 21 '14 at 23:28
4
$ sed 's. ..g' <<< $text
namewithspace
Zombo
  • 1
  • 5
  • 43
  • 62
3

a special case when you need for a variable that has a number:

sh:

typeset -i A=B #or
typeset -i A="   23232"

ksh:

typeset -n A=B #or
typeset -n A="   23232"
muru
  • 69,900
  • 13
  • 192
  • 292
Tagar
  • 243
  • 3
  • 9