首页 > 解决方案 > 如何在 linux bash shell 中定义 $i?

问题描述

我在使用 bash shell 时遇到问题。这是我的linux命令代码:

for i in `cat linshi`;do sed -i '/$i/d' a.txt;done

linshi的内容是:

aa
bb

a.txt的内容是:

aa:wwersdf12314231234
bb:weorpius2345234523
cc:ertoiu230498234098
dd:234092834asdfkdfkg

我想删除a.txt的第一行和第二行。

但不幸的是,我发现 '/$i/d' 不正确。我试过'/\$i/d'和'/"\"$id/',但它们又失败了。谁能帮我?

标签: linuxbash

解决方案


不要使用单引号,而是使用双引号。''不进行任何变量扩展,但双引号会。

这将起作用:

for i in $(cat linshi);do sed -i "/$i/d" a.txt;done

推荐阅读