how can i use variables inside this sed command?

sed -i.bak -e '/triggerword/,${w $DIR/tempfile' -e 'd;}' $DIR/targetfile

inside -e '{w $DIR/tempfile' -e 'd;}' the $DIR/tempfile isn't recognized

marked as duplicate by steeldriver, Kusalananda sed Mar 7 at 11:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You need to search for shell quoting, this has been asked before. Note also you're trying to use nested single quotes, which won't work as expected (they can't be nested, so you end up with two quoted strings with text inbetween). – wurtel Mar 7 at 10:55
  • ok i don't understand. is it possible to use variables in this case or not and what would it look like? – diggidre Mar 7 at 11:12
  • @wurtel They are not using nested single quotes. – Kusalananda Mar 7 at 11:13
  • @diggidre As per the answers to the duplicated questions: -e "{w $DIR/tempfile" -e 'd;}' – Kusalananda Mar 7 at 11:15
  • OK sry i messed up a bit... next step... sed -i.bak -e '/triggerword/,$ "{w $DIR/tempfile" -e 'd;}' $DIR/sourcefile sed -n '/triggerword/,$p' $DIR/tempfile >> $DIR/targetfile sed -i 's/triggerword/#/g' $DIR/targetfile Above gives syntax error: unterminated quoted string When I use sed -i.bak -e '/triggerword/,${w /tmp/tempfile' -e 'd;}' $DIR/sourcefile works like a charm but without variable – diggidre Mar 7 at 11:31