-2

I have the following variable TABLES with multiple lines

TABLES="foobar
test
bar
foo
"

I have a variable SKIP with this sed command

$ echo $SKIP
/bar/d;/foo/d;

I want to use it on sed, like this

echo $TABLES | sed "$SKIP"

But this returns nothing! If I replace the variable with the current content it works

What I am doing wrong?

Rodrigo
  • 135
  • 4
  • 45
  • 107

1 Answers1

-3

What I am doing wrong?

You are not quoting variable expansion. Unquoted variable expansion undergo word splitting, which splits the result of expansion also on newlines, changing it into words. You are doing:

echo foobar test bar foo | ...

Because echo joins the arguments with space, this is effectively one line. /bar/d removes that line.

Quote variable expansion to prevent word splitting. Check your scripts with shellcheck.

echo "$TABLES" | sed "$SKIP"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111