sed 引用变量的问题

  1. file:
Hello World
This is line 2
This is not var \$var
  1. scripts:
var="Hello"
sed -n 's/$var/Hi/p' file
# result: This is not var \Hi

sed -n "s/$var/Hi/p" file
sed -n 's/'$var'/Hi/p' file
sed -n s/$var/Hi/p file
# result: Hi World
  1. 解析
    单引号属于强引用,它会忽略所有被引起来的字符的特殊处理,被引用起来的字符会被原封不动的使用
    所有如果要引用变量,不能使用单引号,而要用双引号
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章