I have a Ubuntu 16.04 Nginx environment with a few minimal WordPress sites (virtually all with up to 5 conventional plugins, 10 pages, 10 images, and a simple contact form to send only textual data).

I daily execute the script cron_daily.sh with the following three loops, from crontab, to update all WordPress apps under document root. The script uses the WP-CLI shell extension.

for dir in ${drt}/*/; do cd ${dir} && wp plugin update --all --allow-root; done
for dir in ${drt}/*/; do cd ${dir} && wp core update --allow-root; done
for dir in ${drt}/*/; do cd ${dir} && wp theme update --all --allow-root; done

${drt} is document root. It was already declared outside permanently, with its file sourced.

I was looking for a way to unite the behavior of these three loops into one segment.

This pattern seems promising, and is based on this example:

for dir in ${drt}/*/; do
    if pushd ${dir}; then
        wp plugin update --all --allow-root
        wp core update --allow-root
        wp theme update --all --allow-root

Is this the shortest pattern one could use? How would you do that?


1 Answer 1


Why three times the same loop instead of one loop like in your example?

At first glance I don't see how this could get any shorter — nor why it should.

If anything, the script could get better (and thus longer) by improving the detection of WordPress (if needed). Also, I'd probably run wp language core update as well to make sure translations are up-to-date.

  • Thx. Improving the detection of WordPress? Feb 8, 2018 at 14:10
  • I now understand you probably meant to wp core is-installed. Feb 8, 2018 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .