skip to Main Content

I would like to extract database access information from a wordpress wp-config.php file.

Example (double quotes):

define( "DB_NAME", "mydatabase" );

But it might as well look like this (single quotes)

define( 'DB_NAME', 'mydatabase' );

A mixed version is also possible:

define( 'DB_NAME', "mydatabase" );

So far I have found this on the internet:

DBNAME=`cat wp-config.php | grep DB_NAME | cut -d ' -f 4`

This works when single quotes are used. What I am looking for is a regex that would handle all cases.

Thank you

2

Answers


  1. Input file

    $ cat wp-config.php
    define( 'DB_NAME', "mydatabase" );
    

    or

    $ cat wp-config.php
    define( 'DB_NAME', 'mydatabase" );
    

    With :

    php -r 'require_once("wp-config.php"); echo DB_NAME;'
    

    With GNU grep:

    $ grep -oP "^define.*?DB_NAME.*?[4247]K[w.]+" wp-config.php
    

    Output

    mydatabase
    
    Login or Signup to reply.
  2. How about awk ?

    Your example File:

    $ cat wp-config.php
    define( 'DB_NAME', "mydatabase.com" );
    

    The Command:

    awk -F "['"]" '/DB_NAME/{print $4}' wp-config.php
    

    The Output:

    mydatabase.com

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search