I have never used PHP with CLI, but I have seen scripts running with PHP code.
I was wondering:
- Why should we use BASH, when PHP is so popular and is able to run in CLI?
- What are the pros and cons with each one?
- Should I use PHP for all CLI scripting in the future?
Bash is more common for this type of application, simply by virtue of it being around forever. But when it comes to choosing one over the other. The language you already know is usually the better choice. Just because you will be more productive.
PHP is only better in the web directory. Bash is better suited for handling command line inputs and general shell semantics, IMO.
I guess the main thing is: bash is always there on a recent unix system (its predecessors really are always there, even on ancient unixen). Bash uses every little utility on the system, which can include PHP, Python, Ruby, AWK, Perl (those last two especially).
Consider: how do you get PHP installed? Bunch of bash scripts and a makefile, right? Or your OS package manager, which these days is probably Python, but used to be a bash script as well.
You can't administer unix without knowing the shell really well, nor can you write or use makefiles. It may not be the right answer every time, but for a scripting job I always try to figure out if it can be done in bash first.
Preference mainly.
Higher level languages (like php) have better data structures easily accessible (among other things). However, it's pretty easy to hack together a working bash script.
So really, it's up to you.
It is still much easier to perform many common tasks in bash. Consider writing PHP equivalents for the following bash one-liners:
I was looking into this myself as I'm current doing a upgrade a on large LMS having come across this link and another which has some result to a proformance test I thought I would add this link to this post. while it task is nota real world example it does show some interesting stats.