Jimmy
Jimmy

Reputation: 12487

Replace config file variables using bash script

I tried to append a value to the ssh config file but it seems that it doesn't work if specify two different values, it reads the first not the second.

It seems sensible, instead of trying to re-write the entire file, to just modify the value.

Currently the value is:

Port 22

I want to go in and change the value to:

Port 222

The best I can come up with is this:

sed -c -i "s/\(Port *= *\).*/\1$REPLACEMENT_VALUE/" /etc/ssh/sshd_config

However I know this isn't right because it's working in a way where it is expecting to have an equals sign between the value and the variable.

I need the script to do something like this:

Upvotes: 0

Views: 4031

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

I think you mean this,

sed 's/.*\bPort\b.*/REPLACEMENT_VALUE/' file

Example:

$ REPLACEMENT_VALUE="Port 222"
$ echo -e "foo\nPort 22" | sed "s/.*\bPort\b.*/$REPLACEMENT_VALUE/"
foo
Port 222

Upvotes: 3

Related Questions