<分区>
Possible Duplicate:
How to trim whitespace from bash variable?
我已经搜索并尝试了很多解决方案,但似乎没有一个对我有用......
我有一个 shell 变量,由于前导和尾随空格而导致问题。我们如何使用 shell 脚本去掉一行中的所有空格?
<分区>
Possible Duplicate:
How to trim whitespace from bash variable?
我已经搜索并尝试了很多解决方案,但似乎没有一个对我有用......
我有一个 shell 变量,由于前导和尾随空格而导致问题。我们如何使用 shell 脚本去掉一行中的所有空格?
最佳答案
我可以想到两种选择:
variable=" gfgergj lkjgrg "
echo $variable | sed 's,^ *,,; s, *$,,'
否则
nospaces=${variable## } # remove leading spaces
nospaces=${variable%% } # remove trailing spaces
关于bash - shell 脚本 : How to trim spaces from a bash variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11791402/