Skip to content
Advertisement

How to insert variable containing a path into sed

I’m writing a linux script, which is to replace a string for a file. Here is my script:

#!/bin/sh
var=./video/example.mp4
`sed -i '' 's/sceneFilePath:.*/sceneFilePath: "$var",/g' test`

In the file named test, there is a line like this:

sceneFilePath: "t.mp4",

What I need is to replace this line with the line below:

sceneFilePath: "./video/example.mp4",

It means that things between double quotes should be replaced by the $var.

However when I execute my script, I get sceneFilePath: "$var",.

I’ve read this answer: Replace a string in shell script using a variable

However, I get an error:

sed: 1: “s/sceneFilePath:.*/scen …”: bad flag in substitute command: ‘v’

Advertisement

Answer

var='./video/example.mp4'
sed -i '' "s/sceneFilePath:.*/sceneFilePath: "$var",/g" test

Else:

var='./video/example.mp4'
sed -i '' "s#sceneFilePath:.*#sceneFilePath: "$var",#g" test
Advertisement