How to grep for Wordpress DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php
This grep statement filters out the DB_NAME, DB_USER, DB_PASSWORD and DB_HOST contents from a wp-config.php:
example-3.txt
For example, from the wp-config.php line
example-2.php
it will extract techoverflow.
Here are the statements for DB_NAME, DB_USER, DB_PASSWORD and DB_HOST:
example-1.txt
DB_USER=$(grep -oP "define\(['\"]DB_USER['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php)
DB_PASSWORD=$(grep -oP "define\(['\"]DB_PASSWORD['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php)
DB_HOST=$(grep -oP "define\(['\"]DB_HOST['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php)You can use this, for example, in automatic backup scripts without manually copying the password to the backup script
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow