0

When trying to extend a script, for some reason, if a sed expression is inside a variable it won't run.

Minimal example:

echo lorem >> /tmp/testsed
CMD="sed --debug -i.bak -e 's/lorem/ipsum/' /tmp/testsed"
$CMD

results in:

sed: -e expression #1, char 1: unknown command: `''

I've looked at a few (1, 2 - not quite the same, 3 and 4 - solves the issue, but not ideal ) similar posts, but none solved the issue.

For now I'm using eval (like 4 initially suggests), but it's not an ideal solution; also creating a function is not an option.

I feel like it's related to having to escape some characters on the expression, but from my tests I could not find which.

This sed expression is set as a action that another script executes after some specific action.

Luctins
  • 11
  • there is no option other than things you limited yourself to don't use them, and you cannot execute a command within a variable with just doing $CMD – αғsнιη Apr 30 '21 at 19:15
  • @Luctins The accepted answer on the linked question (in particular "Below, $abc is split on whitespace, and ls gets the three arguments -l, "/tmp/test/my and dir" (with a quote at the front of the second and another at the back of the third).") is what you're looking for. It's just that in your case, the single quotes around your sed expression are being passed to sed. When you run that command from the prompt on your own, the shell itself removes the single quotes before passing the expression to sed. – Wieland Apr 30 '21 at 19:19

0 Answers0