Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variables in a sed command

Tags:

linux

bash

I'm trying to add some text (a path) at the end of a line which is found by a sed command:

var="/folder1/folder2/folder3"

sed -i "/Begins with this text/s/$/$var/" filename

I know that double quotes are needed to use variables in a sed command but if I use the above command it gives me an error message saying:

expresssion #1, character 23: unknown option to `s

What am I doing wrong?

like image 491
Ozkan Avatar asked Jan 12 '23 08:01

Ozkan


1 Answers

Change the delimiter in the substitute command to something that won't appear in $var, e.g.

sed -i "/Begins with this text/s|$|$var|" filename

or escape the slashes in $var:

var="\/folder1\/folder2\/folder3"
like image 138
Barmar Avatar answered Jan 19 '23 12:01

Barmar