I want to replace the contents between two lines specified by line numbers using bash. For example. I have a file sample.txt, it has the following contents.
apple
peach
orange
juice
toy
computer
phone
I have a variable called var
var="cup\nbook"
I want to replace the contents between the second line and the 5th line(including the two lines) in sample.txt with the contents stored in var, so the final contents after the modification in file sample.txt should be
apple
cup
book
computer
phone
How to do that?