Drupal - Change module settings stored in an array with drush

Drush does not have any mechanism to convert commandline arguments into php arrays for the variable-set command; however, it is easy to write short php snippets using the php-eval command ('ev' for short).

For example, if you have set up a site alias for your site called "@site":

$ drush @site ev 'variable_set("addtoany_nodetypes", array("about_us" => "about_us", "contact" => "contact", "page" => "page", "legal" => 0, "webform" => 0));'
$ drush @site vget addtoany_nodetypes
addtoany_nodetypes: Array
(
    [about_us] => about_us
    [contact] => contact
    [page] => page
    [legal] => 0
    [webform] => 0
)
$ drush @site ev '$v = variable_get("addtoany_nodetypes", array()); $v["page"] = 0; $v["legal"] = "legal"; variable_set("addtoany_nodetypes", $v);'
$ drush @site vget addtoany_nodetypes
addtoany_nodetypes: Array
(
    [about_us] => about_us
    [contact] => contact
    [page] => 0
    [legal] => legal
    [webform] => 0
)

If you are not using a site alias, then cd to the Drupal root of your site (or cd to the sites folder that contains settings.php if using a multisite) and remove the "@site" from the examples above.


As stated in the drush help documentation drush help variable-set the recommended way to set an array variable is using format=json:

php -r "print json_encode(array('drupal', 'simpletest', 'leftandright', 'category'));"  | drush vset --format=json project_dependency_excluded_dependencies -

Don't forget the "-" at the end.

Tags:

Drush