Use a variable in a sed command

后端 未结 6 1361
梦谈多话
梦谈多话 2020-12-09 03:15

I can\'t seem to use a variable in a sed command, for example:

sed \"24s/.*/\"$ct_tname\"/\" file1.sas > file2.sas

I want $ct_tnam

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 03:53

    Other answers focus on the use of escaped double quotes in their examples. Note that this is not always what you want :

    $ FOO="auie"; echo foo123bar|sed "s/123/\"$FOO\"/"
    foo"auie"bar
    $ FOO="auie"; echo foo123bar|sed "s/123/$FOO/"
    fooauiebar
    $ FOO="auie"; echo fooauiebar|sed "s/\"$FOO\"/123/"
    fooauiebar
    $ FOO="auie"; echo fooauiebar|sed "s/$FOO/123/"
    foo123bar
    

提交回复
热议问题