0

I want to insert a text from variable at the certain line of a text file.

$cat foo
hello
hello
$var=`cat foo`
$echo "$var"
hello
$cat bar
some
text

I want to insert $var into second line, but sed does not read the content of the variable:

sed -i -e '2i$var\' bar
hello
$var
world

I guess because of two identical words in foo, I'm getting this:

sed: -e expression #1, char 11: extra characters after command
Josef Klimuk
  • 1,636

1 Answers1

3

You call variables in sed with double quotes

sed -i -e "2i$var" bar
cmak.fr
  • 8,976