2012-08-03 38 views

回答

13

我能想到的兩個選項:

variable=" gfgergj lkjgrg " 
echo $variable | sed 's,^ *,,; s, *$,,' 

否則

nospaces=${variable## } # remove leading spaces 
nospaces=${variable%% } # remove trailing spaces 
+1

'nospaces = $ {變量//}實際上刪除了我的Bash中的前導和尾隨空格 – 2012-08-03 09:01:52

+5

nospaces = $ {variable //}刪除所有空格 – Harpreet 2014-01-05 21:19:13

+1

nospace s = $ {variable //}刪除所有空格 – gammay 2016-04-12 09:25:46

1

有這麼多的方式來實現這一目標,AWK oneliner:

kent$ echo " foo - - - bar "|awk '{sub(/^ */,"",$0);sub(/ *$/,"",$0)}1' 
foo - - - bar 
相關問題