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
:
grep -oP "define\(['\"]DB_NAME['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php
For example, from the wp-config.php
line
define('DB_NAME', 'techoverflow');
it will extract techoverflow
.
Here are the statements for DB_NAME
, DB_USER
, DB_PASSWORD
and DB_HOST
:
DB_NAME=$(grep -oP "define\(['\"]DB_NAME['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php)
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