The exec command in Linux is used to execute the Shell command, without creating any new process instead uses the currently running process to execute any command. For instance, we run a command with the exec command from the terminal, where that command will use the terminal process rather than creating a new process. The exec command is useful for older computers having fewer system resources as it uses the existing processes and consumes no processor to execute commands.
This guide will cover the exec command using these helping topics:
- How to Use the exec Command in Linux?
- exec Command Options
- Basic Use of the exec Command
- Use the exec Command Within the Shell Script
- Redirect Commands To a File Using the exec Command
- Use the exec Command to Print in an Empty Environment
How to Use the exec Command in Linux?
This section will demonstrate how the exec command works with examples. Let’s first discuss the general syntax of the exec command:
$ exec <options> <command> <redirection>
The above command explanation is:
- exec: It uses the exec command, and the command written with exec is passed as an argument.
- options: Add the exec command available option here.
- command: Put any command where you want to run without creating any new process.
- redirection: If no option or command is passed to the exec command, it redirects the input to the current shell.
Note: Any command written with the exec command is used as an argument for the exec command.
exec Command Options
The exec command has the following built-in options:
|-c||This option executes the exec command in an empty environment.|
|-l||The dash is passed as the zeroth argument to the exec command.|
|-a [name]||The [name] is passed as the zeroth argument to the exec command.|
Let’s understand the exec command with examples.
Example 1: Basic Use of the exec Command
To understand the working of the exec command, let’s understand it using a simple example. The “ps” command lists the currently running processes, and “echo $$” shows the process ID for the current working shell (terminal):
$ ps $ echo $$
The output shows that two processes are currently working. The shell in which the user is currently working has process ID “5676”
Let’s pass the command “sleep 50” to the exec command as the argument and check the process ID of this command from another terminal. Pass the sleep 50 commands to the exec command by running this command:
$ echo $$ $ exec sleep 50
Now, run the below command in a new terminal to check the process ID of the exec command, which shows that its Process ID is “5676”. It verifies that the exec command uses the previous process rather than opening a new process:
$ ps -ae | grep sleep
The exec PID is “5676”, the same as the previously running program. No new process has been started.
Example 2: Use the exec Command Within the Shell Script
The commands or shell script can be used within the Shell script with the help of the exec command to use already running processes. Using the existing processes to run new commands protects the system from creating new processes and uses fewer system resources.
For instance, the “df -h” and “ls” commands are used within the while loop using the bash script named “testscript.sh” to run these commands by using the existing processes:
#! /bin/bash while true do echo "1. Disk Usage " echo "2. List File & Directories" read Input case "$Input" in 1) exec df -h ;; 2) exec ls ;; esac done
To run the bash script, use the following command:
$ bash testscript.sh
The user enters the number “2”, which runs the ls command without creating any new process.
Example 3: Redirect Commands To a File Using exec Command
We can use the exec command to redirect the commands to a file. For instance, the “exec” command redirects all the commands to the ”test” file until we exit the terminal:
$ exec > test $ echo "Before Command." $ ls $ echo "After Command" $ exit
After running the exit command, the user exits the terminal, and the entered commands output is saved to the “test” file.
To check the content of the “test” file, use the below cat command:
$ cat test
The output shows that the output of three commands entered after the exec command is stored in the “test” file.
Example 4: Use the exec Command to Print in an Empty Environment
The “c” option of the exec command is used to execute the command in an empty environment. For example, let’s start the bash shell and print the existing Environment variables by running these commands:
$ bash $ printenv
The output shows the list of environment variables in the bash shell.
Let’s run the same command with the exec command with the “c” option to run it in an empty environment:
$ bash $ exec -c printenv
The output shows that it prints no output, which verifies that it runs in an empty environment.
This exec command runs any command without creating any new process. It uses the existing process to run the commands or scripts passed to the exec command. This article has covered the exec command with examples.