Path substitution with sed and shell variables in OS X

I am on Mac OS X and use sed to replace in place.

Essentially, I have this:

#!/bin/sh -e
PREFIX="$1"

sed -i bak -e 's|OCAMLDIR|"${PREFIX}"|g' ocamloptrev

Where PREFIXis the way, so I use |.

Unfortunately, the variable in the file path does not get an estimate, as I expected, in the end I get:

OCAMLC="${PREFIX}"/bin/ocamlopt

How can I get the correct score ${PREFIX}in the team sed?

+4
source share
2 answers

Try the following:

#!/bin/sh -e
PREFIX="$1"

sed -i bak -e 's|OCAMLDIR|'"${PREFIX}"'|g' ocamloptrev

What you basically do is “go out” / go out of one quotation mark, enter a string with two quotation marks, interpret the variable inside double quotation marks, and re-enable single quotation marks.

, :

#!/bin/sh -e
PREFIX="$1"

sed -i bak -e "s|OCAMLDIR|${PREFIX}|g" ocamloptrev

("") , . Bash .

3.1.2.2

('') . , .

3.1.2.3

('") , $, `, \ , , !. $ ` (. )....

+7

( , ), - , ${PREFIX} , .. :

sed -i bak -e 's|OCAMLDIR|'"${PREFIX}"'|g' ocamloptrev

:

sed -i bak -e "s|OCAMLDIR|${PREFIX}|g" ocamloptrev

, ( , ). , .

.bak, bak .

+3

Source: https://habr.com/ru/post/1625672/


All Articles