0

My Aim is to replace the value of some variables by identifying the variable name and by using sed.

However, i'm not getting the desired output, the value instead of getting replaced is actually getting appended.

Input file content:

export TENANT_NAME=xxxx
export WONDER_VALUE=xxx_xxx_xxxx_xxxx
export LOAD_BALANCERS=xxxx_xx

Bash Script:

#!/bin/bash

sed "s/${1}=*/${1}=${2}/" input.env >> input.env

Command:

./replaceValue.sh WONDER_VALUE BOB_ROB_ALICE

Result which I get:

export TENANT_NAME=xxxx
export WONDER_VALUE=BOB_ROB_ALICExxx_xxx_xxxx_xxxx
export LOAD_BALANCERS=xxxx_xx

Result I expect:

export TENANT_NAME=xxxx
export WONDER_VALUE=BOB_ROB_ALICE
export LOAD_BALANCERS=xxxx_xx
theborngeek
  • 121
  • 1
  • 7

1 Answers1

-1

The error lays in the regex: unlike in the shell, the * alone doesn't match anything; you mean .* instead. In the regex you wrote, =* matches only one of more occurences of =; this explains why the rest of your line was not matched up to the end.

And if you want to modify a file with sed, use the -i option.

Try:

Bash Script:

#!/bin/bash

sed -i "s/${1}=.*/${1}=${2}/" input.env
Pierre François
  • 5,850
  • 1
  • 17
  • 38