Shell脚本:删除字符串中的所有空格字符

Bar*_*Bar 3 shell

我在shell脚本上工作,我想删除字符串中的所有空格字符.在另一个问题上,我看到了一个sed替换命令,并尝试使用它来发送空字符:

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

但它没有用.

还有其他办法吗?

Sto*_*ica 13

这将删除输入中的所有空格字符:

echo some text with spaces | tr -d ' '
Run Code Online (Sandbox Code Playgroud)

另一种使用方式sed:

echo some text with spaces | sed -e 's/ //g'
Run Code Online (Sandbox Code Playgroud)

但是......在你的例子中没有空格,看起来你想用变量的内容替换空格$text......所以不是100%肯定这是你正在寻找的.如果没有,那么请澄清.