Skip to content
Advertisement

Shell substring with sed in a variable

I have tried this command in bash linux

echo "example=value" | sed "s/^example=(.*)$/1/"

The output is value. But if I put it in a variable, it doesn’t work.

For example:

var="example=value" | sed "s/^example=(.*)$/1/"
echo $var

The output is nothing. What wrong?

Advertisement

Answer

Do it like this:

var=$(echo example=value | sed "s/^example=(.*)$/1/")
echo $var
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement