Search code examples
linuxwordpressbashgit-bashwp-cli

How to run WP-CLI using bash script in linux


I'm having issue to run bash script setting up WP-CLI by it's own. Keep on getting wp no command found error. Please help.

#!/bin/bash
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
exec bash
wp --info
wp plugin install taxonomy-terms-order --path=/var/www
wp plugin activate taxonomy-terms-order --path=/var/www

It's only running till exec bash line. after that its not installing any plugin. Please help.


Solution

  • Do not hesitate to make small experiments to understand the problem:

    $ cat test.sh 
    #!/bin/bash
    
    echo "Test 1"
    exec bash
    echo "Test 2"
    $ echo $$
    6506
    $ ./test.sh 
    Test 1
    $ echo $$
    6548
    

    exec bash is opening a new blocking process.

    So, I think you can remove this line from your script.

    If /usr/local/bin is not in your PATH, you can use the complete path of /usr/local/bin/wp instead of wp :

    /usr/local/bin/wp --info
    /usr/local/bin/wp plugin install taxonomy-terms-order --path=/var/www
    /usr/local/bin/wp plugin activate taxonomy-terms-order --path=/var/www
    

    Or you can add this path to the PATH:

    export PATH="${PATH}:/usr/local/bin/wp"