1

I need to change the same file on multiple servers and replace it with the hostname of the server.

sed -i 's/localhost/myhostname/g' /var/www/config.php

Is what I currently do.

How would I turn it into a script so that it will automatically check what the servers hostname is and replace "myhostname" with the servers hostname?

1 Answer 1

6

Switch to double quotes and you can add a variable or command substitution.

sed -i "s/localhost/$HOSTNAME/g" /var/www/config.php
sed -i "s/localhost/$(hostname)/g" /var/www/config.php
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.