-1

I want to use 'sed' command (for example) that have two variables. One should be evaluated and other not.

For example, var1="should be evaluated" var2="should not be evaluated"

echo "should be evaluated" | sed 's|${var1}|$var2|g'

I want to see: $var2 (not value)

How to do it ? My goal is to replace one string that I get as parameter to variable name.

The problem is that a double apostrophe (") evaluated the variable and a single spostrophe (') not. And I can't understand how to use it in the same command.

Thanks, Alex

Alex
  • 29
  • 5
  • 1
    You can have single-quoted strings and double-quoted strings adjacent to each other (without any blanks between them). See [this answer](https://stackoverflow.com/a/13802438/2554472). – Mark Plotnick Feb 04 '23 at 11:19

1 Answers1

0

Modify your sed like this:

#!/bin/bash

var1="should be evaluated"
var2="should not be evaluated"

echo "should be evaluated" | sed "s|${var1}|\$var2|g"
  • use double quotes in the sed to variables are evaluated.
  • but backslash $var2 to it is not evaluated. \$var2 then becomes the character $ followed by text var2.
Nic3500
  • 8,144
  • 10
  • 29
  • 40