2017-10-06 58 views
0

我有這樣一個變量:(在新行中的每個字)在bash變量刪除所有文字occurence

> echo $LIST 
toto 
toto2 
titi 
rererer 
dfs 
sdfsdf 
titi 
titi 

我嘗試刪除的「蒂蒂」所有出現,以獲得:

> echo $LIST 
toto 
toto2 
rererer 
dfs 
sdfsdf 

我試圖與LIST = $(回聲$ {// LIST蒂蒂/})併除去它,但它也刪除新行,並得到這樣的結果:

> echo $LIST 
toto toto2 rererer dfs sdfsdf 

我的問題是如何刪除所有出現保持每個單詞在一行嗎? 感謝提前:)

+0

子shell和回聲是多餘的 – 123

+0

你可以嘗試:echo「$ list」| grep -v titi –

回答

2

你需要把周圍"${LIST//titi/}"引號,否則空白將被摺疊:

 
$ LIST='toto 
> toto2 
> titi 
> rererer 
> dfs 
> sdfsdf 
> titi 
> titi' 
$ echo "${LIST//titi/}" 
toto 
toto2 

rererer 
dfs 
sdfsdf 


但你也可以只直接分配:

LIST=${LIST//titi/} 
echo "$LIST" # quotes are important here!