replacing values in shell script using sed My shell script looks like this:

FAILED_REQUEST_DIRECTORY=/bla/bla2 \
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234 \
     -jar /xyz/abc-service-1.0-SNAPSHOT.jar \
     --server.port=1111 \
     --server.address=127.0.0.1 \
     --kafka.bootstrap.servers=10.111.11.11:3197,10.112.12.111:3197,10.123.44.25:3197 \
     --topic.pushcrew.encryptionKey=abc-def-egh \
     --spring.application.name=name-sevice \
     --fb.verification.token=token \
     --management.trace.include.payload=true \
     --topic.pushcrew.hits=activity-logs \
     --max.block.ms=1000 \
     --pager.duty.api.key=apikey \
     --dsn=dsn


If I have to replace values of keys in the command, for ex: replacing value of key --server.address with the value to shell script through a variable, how would I do it?

I understand it would look like following if I had to replace value for key --server.address:

sed -i "s#--server.address=.* #$SERVER_ADDRESS#g;


But how do I maintain the space that has to be there after the replacement and how do I make sure that part int the key only after =(equals to) and before   (space) gets replaced.
Negation to the rescue:

sed -i "s/server.address=[^\ ].*\ /server.address=${SERVER_ADDRESS} /g"


This is telling to search zero or more characters (.*) NOT matching whitespace ([^\ ]) up to the first whitespace.

A good pattern is not to use real values to be replaced (10.111.11.11:3197) but to use a mark like ###SERVER_ADDRESS###. If something goes wrong you can easily post-check with

grep "###" &amp;&amp; exit -1


This can avoid continuing with dangerous values (mixing dev/pre/pro values)