1

When using mk, GNU sed, I find that variables do not expand at all.

Problematic code:

VAR=qux
...
build:
    sed -i "s|FOO = .*|FOO = $VAR|" bar.file

This seems to run the following:

sed -i "s|FOO = .*|FOO = $VAR|" bar.file

when I want it to run

sed -i "s|FOO = .*|FOO = qux|" bar.file

I have tried using single quotes and double quotes, as other stack exchange posts have mentioned. The only instance I can get $VAR to expand is when there are no quotes around it. Which of course means that sed no longer sees the expression as an expression.

closed as off-topic by Michael Homer, Stephen Rauch, Archemar, Rui F Ribeiro, αғsнιη Aug 21 '17 at 11:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Michael Homer, Stephen Rauch, Archemar, Rui F Ribeiro, αғsнιη
If this question can be reworded to fit the rules in the help center, please edit the question.

  • If this is bash then put set -x before the sed call. That shows you what the shell executes. – Hauke Laging Aug 20 '17 at 8:48
  • "a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed)" -- This is not the case, the case was the quoting in my original. The quoting is not a form of typo as it was done intentionally. This problem caused me much strife and no help was available anywhere on the internet. This problem is reproducible. – Finn O'leary Aug 21 '17 at 11:38
2

This looks like a fragment in a Makefile.

There's two errors in the file:

  1. The sed substitution command uses | as the delimiter, but the middle delimiter is /.

  2. The Makefile variable VAR is dereferenced as $VAR instead of $(VAR) or ${VAR}.

Note that a Makefile is not a shell script. Therefore, the following Makefile is perfectly valid and will produce the string qux world as output on the terminal:

VAR=    qux                                        

build:                                             
        @echo 'hello world' | sed 's|hello|$(VAR)|' 

In a Unix Makefile, $VAR would expand to the value of the variable V followed by the letters AR. This is because unless otherwise stated, variables' names are a single character. If you have multi-character variable names, you must enclose the whole name in either $(...) or ${...}.

Obviously, Plan9 Makefiles may be different in that respect.


UPDATE:

The following mkfile outputs the correct value when using mk 2.0, hello = qux:

VAR=qux

all:
        echo "hello = bar" | sed "s/= bar/= $VAR/"

So the quoting in the question seems correct to me.

0

It turns out that, while I had previously tried escaping it, I erroneously put double quotes around it like so:

sed -i 's|FOO = .*|FOO = '"$VAR"'|' bar.file

Changing it to

sed -i 's|FOO = .*|FOO = '$VAR'|' bar.file

works.

  • 2
    It shouldn't work since the sed command is wrong. The middle / should be |. – Kusalananda Aug 20 '17 at 6:20
  • Thanks, this was an error in the example I gave, not in the original code. I've corrected it. – Finn O'leary Aug 21 '17 at 11:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.