Php vs bash for CLI scripts?

I have never used PHP with the CLI, but I have seen scripts being executed with php code.

I was wondering why we should use bash when php is so popular and can work in the CLI.

What are the pros and cons with each of them?

Should I use php for all CLI scripts in the future?

+7
command-line-interface bash php
source share
8 answers

In bash, it is still much easier to complete many common tasks. Consider writing PHP equivalents for the following single-line bashs:

 # remove a folder full of rubbish
 rm -rf oldTrashFolder

 # create an archive of files containing the word 'localhost'
 tar cf backup.tar $ (grep -rlw localhost ~ / my-source-code)

 # make a copy of a mysql database
 {echo "create database NEWDATABASE;";  mysqldump OLDDATABASE;  } |  mysql NEWDATABASE
+7
source share

Bash is more common for this type of application, simply because it resides forever. But when it comes to choosing one over the other. A language that you already know is usually your best bet. Just because you will be more productive.

+4
source share

PHP works best in a web directory. Bash is better suited for handling command line inputs and general shell semantics, IMO.

+3
source share

I think python is better than PHP for the CLI.

If you've never used PHP, I recommend Python because PHP was originally designed for web development to create dynamic web pages, and for Python it was more generous and emphasized code readability.

+3
source share

For general-purpose scenarios, I suggest going through Bash completely.

Bash is probably the most common shell, and your script will work on most * nix systems. In contrast, PHP will not always be easily accessible. Of course, if you are not going to use these scripts in comparison with an unfamiliar system, you will be fine with both.

However, most standard shell commands are easier to write in Bash and then in php. In php, you have to open subprocesses, execute shell commands through some wrapper functions or something not in Bash, you would just write your Bash session in a file, making it executable. Take a directory listing, for example, in Bash just specify ls , and in php you will probably use scandir which is not so much fun. I would also suggest that scandir has much fewer sorting options. grep -Ri something . is anyone

Most general-purpose languages ​​can be used for shell scripts, but this does not mean that they should be, and in the end it comes down to task execution and subjective preference.

+2
source share

I assume that the main thing: bash is always present in the recent unix system (its predecessors really always exist, even on the ancient unixen). bash uses every small utility in the system, which can include PHP, Python, Ruby, AWK, Perl (especially the last two).

Consider: how do you install PHP? A bunch of bash scripts and a make file, right? Or your OS package manager, which these days is probably Python, but also had a bash script.

You cannot administer unix without knowing that the shell is really good, and you cannot write or use makefiles. This may not be the right answer every time, but for scripting, I always try to figure out if this is possible in bash.

+2
source share

Preference is basically.

Higher-level languages ​​(e.g. php) have better data structures that are easily accessible (among other things). However, it's pretty easy to hack a working bash script.

So it is up to you.

+1
source share

I studied this myself, since I am currently doing an update on a large LMS, having come across this link, and another that has some result for a performance test. I thought I would add this link to this . while this task is not an example of the real world, it shows some interesting characteristics.

-one
source share

All Articles