How to use sed to replace an environment variable
24,950
This is a case for some ugly fancy quoting. Single quotes stop shell parameter expansions, and double quotes to allow them.
You can think of quotes as on-off switches in a sed
expression, closing one type of quoting and opening another like this: 'strong'"weak"'strong'
Using $USER
as an example:
$ cat file
$USER
some text ($USER)
$ sed 's/($USER)/('"$USER"')/' file
$USER
some text (zanna)
Related videos on Youtube
Author by
user_mda
Updated on September 18, 2022Comments
-
user_mda over 1 year
I have the following strings in a file
$ENVVAR someText ($ENVVAR)
I want to use
sed
so that only the second occurence of the variable is replaced with the value of the environment variableso
$ENVVAR someText (value of env variable)