


Grep "search term" /path/to/nonexistentfileĮcho "Performing operation 3 (stdout)." # Redirect stdout and stderr to the log fileĮcho "Performing operation 1 (stdout)."Įcho "Performing operation 2 (stderr)." Here, I will be using 3 arguments, two for the standard output and one for standard error. Yet another interesting and easy implementation of the exec is where you can redirect the output to a file. Use exec command in shell scripts for logging That does not seem very practical? Here's another example.Ģ. Simply put, the process will be replaced by the 2nd command argument, and the 3rd command won't be executed.Īnd here's the output if you execute the shown script:Īnd as you can see, the 3rd command statement which was supposed to print "After exec: This line will not be executed" is shown here. Now, the third statement won't be executed as the process was replaced by the exec previously, and there were no additional arguments to support the execution of the third command statement. The second statement involves the usage of the exec command with the ls command to list the contents of the current working directory and it will replace the previous process too! First will print the basic text indicating the original script which is meant to be replaced.

Now, let me explain what this script will do.

Once done, save the changes and exit from the nano text editor. So here, I will be using a simple script that will display how the exec command in the script can be used to replace the current shell process.įirst, use the following command to use the nano to create and edit a new script: nano process_replacement.shĮcho "Before exec: This is the original script"Įcho "After exec: This line will not be executed" Replacing the process is one of the most known implementations of exec in the shell script. Use the exec command to replace process in shell script Seems complex? Let me give you some examples of using the exec command in shell scripts: It replaces the shell process with the specified command. But exec runs the Linux commands without starting a new process. Wait! Don't shell scripts execute Linux commands already? They do. The shell built-in exec command is used for executing commands in shell scripts.
