Bar
Bar

Reputation: 351

Shell script: Remove all the space characters in a string

I work on shell script and I want to remove all space characters in a string. On another question I saw a sed command for replacing and tried to use it with sending null character in it:

echo \0 | sed "s/ /${text}/"

But it did not work.

Any other way to do this?

Upvotes: 4

Views: 38997

Answers (2)

cbdeveloper
cbdeveloper

Reputation: 31485

This was my use case:

Multiple key values that should go on a single string. I'd like to split the string to make them easier to read.

SPLIT="          \
KEY_1=$VALUE_1,  \
KEY_2=$VALUE_2,  \
KEY_3=$VALUE_3   \
"

JOINED="$(echo "$SPLIT" | sed "s/ //g")"  # REMOVE SPACES
echo $JOINED

Upvotes: 0

janos
janos

Reputation: 124734

This deletes all space characters in the input:

echo some text with spaces | tr -d ' '

Another way using sed:

echo some text with spaces | sed -e 's/ //g'

But... In your example there are no spaces, and it looks like you want to replace spaces with the content of the variable $text... So not 100% sure this is what you're looking for. So if not, then please clarify.

Upvotes: 16

Related Questions