Explain the Difference Between shell_exec() and exec() Functions
Last Updated :
12 Aug, 2021
In this article, we will learn about the shell_exec() & exec() functions in PHP. As we know that in order to execute a command in a system, we need the shell of the respective operating systems, but if we need to execute the same command with the help of a programming language like PHP, then we use these two functions. We use any of the two functions but they differ in terms of output, they will give after their successful execution. Please refer to PHP | shell_exec() vs exec() Function.
shell_exec() Function: The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The shell_exec is an alias for the backtick operator, for those used to *nix. If the command fails, it returns NULL and the values are not reliable for error checking. This function gets disabled when PHP runs in safe mode.
Syntax:
string shell_exec( $cmd )
Parameter: shell_exec() function passes only a single argument($cmd) as it holds the command that is to be executed.
Return value: it returns the executed command or NULL if an error occurs.
Example 1:
PHP
<?php
// Command
$command = "DATE";
// Passing the command to the function
$cmd_output = shell_exec($command);
echo $cmd_output;
?>
Output
The current date is 08-07-2021. Enter the new date: (dd-mm-yy)
Example 2:
PHP
<?php
// Command
$command = "TIME";
// Passing the command to the function
$cmd_output = shell_exec($command);
echo $cmd_output;
?>
Output:
The current time is: 21:05:01.82. Enter the new time:
exec() Function: The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output. It also returns NULL if no command runs properly. It differs in terms of the output it gives wrt shell_exec() function. It executes the command and gives all the output of that command in the form of an array and it also gives some extra info that we can use to check whether the command is successfully executed or not.
Syntax:
string exec( $command, $output, $return_var )
Parameters: This function will accept three parameters:
- $command: used to hold the command which is to be executed.
- $output: used to specify the array which will be filled with every line of output from the command.
- $return_var: This parameter is present along with the output argument, then it returns the status of the executed command that will be written to this variable.
Return Value: Return the executed command.
Example 1:
PHP
<?php
// Command to be executed in the shell
$command = "DATE";
// Calling exec function
exec($command, $output_array, $cmd_status);
echo "Command status - ".$cmd_status." <br><br>";
echo var_dump($output_array)
?>
Output:
Command status - 1
array(2) { [0]=> string(31) "The current date is: 08-07-2021"
[1]=> string(30) "Enter the new date: (dd-mm-yy)" }
Example 2:
PHP
<?php
// Command to be executed in the shell
$command = "TIME";
// Calling exec function
exec($command, $output_array, $cmd_status);
echo "Command status - ".$cmd_status." <br><br>";
echo var_dump($output_array)
?>
Output:
Command status - 1
array(2) { [0]=> string(32) "The current time is: 21:38:09.81"
[1]=> string(19) "Enter the new time:" }
Difference between shell_exec() and exec() function:
| shell_exec() | exec() |
---|
1. | The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string.
| The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output. It also returns NULL if no command runs properly.
|
2. | The shell_exec() function provides complete output as a string.
| The exec() function can provide all output in the form of an array specified as the second parameter.
|
3. | The shell_exec() function can return null for both cases when an error occurs or the program produces no output.
| The exec() function can return null only in the case when no command executes properly.
|
Similar Reads
What is the difference between fopen() and fclose() functions in PHP ? In this article, we are going to discuss the differences between fopen() and fclose() functions. fopen() function: This function helps users to open a file or a web URL. It mainly accepts two parameters - One is $file that specifies the file or an URL to be opened and the other is $mode that specifi
2 min read
Difference between die() and exit() functions in PHP PHP exit() Function: In PHP, the exit() function prints a message and exits the application. It's often used to print a different message in the event of a mistake. Use exit() when there is not an error and have to stop the execution. Syntax: exit("Message goes here"); or exit(); Example: exit("This
2 min read
Difference between Shell and Kernel In computing, the operating system (OS) serves as the fundamental layer that bridges the gap between computer hardware and the user. Two critical components of an operating system are the kernel and the shell. Understanding the relationship between these two components is fundamental to grasping how
4 min read
Difference between system() and execl() call A system() call is a way for the programs to interact with the operating system. It is used to execute a command within a process. The system call provides the services of the operating system to the user programs via Application Program Interface (API). It provides an interface between a process an
4 min read
Difference between fork() and exec() Every application(program) comes into execution through means of process, process is a running instance of a program. Processes are created through different system calls, most popular are fork() and exec() fork() pid_t pid = fork(); fork() creates a new process by duplicating the calling process, T
4 min read
Difference Between Bind Shell and Reverse Shell A shell is a program that interprets our commands and gives the written commands to the operating system. It acts as an interface between the user and the operating system. It takes input from the keyboard and gives it to the OS, and the terminal lets you type commands and interact with the shell.Wh
6 min read
Shell Scripting - Difference between Korn Shell and Bash shell Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year bac
3 min read
Difference between Program and Executable File 1. Program : Program, as name suggest, are simply set of instructions developed by programmer that tell computer what to perform and consist of compiled code that run directly from operating system of computer. 2. Executable File : Executable File, as name suggests, are computer files that contain e
2 min read
What is the difference between fopen modes âr+â, "rw+" and âw+â in PHP? File handling is a very important part of programming when it comes to making applications. We might require reading input from a file or writing output into a file. File handling in PHP is similar to other programming languages. There are predefined functions that can be used to accomplish the spec
2 min read
Bash Script - Difference between Bash Script and Shell Script In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capabl
4 min read