4

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?

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.