Uses of Exec Command in Linux


Introduction

The exec command is a built-in command in the Unix and Linux shell that allows a user to execute a command or a script in place of the current shell. This means that the exec command is used to replace the current shell process with a new process, which can be a command or a script. The exec command is useful in shell scripts when you want to execute a command or a script and then exit the current shell.

Syntax of exec command −

Example

$ exec: exec [-cl] [-a name] [command [argument ...]] [redirection ...]

Why should we use the exec command?

One of the main advantages of using the exec command in shell scripts is that it can save system resources. When you run a command or a script using the exec command, the new process created by the exec command runs in the same process space as the current shell. This means that the new process does not require its own process space, which can save system resources.

Another advantage of using the exec command in shell scripts is that it can simplify the structure of your script. When you use the exec command, you can avoid having to create a new process and manage it separately from the current shell. This can make your script simpler and easier to maintain.

In this article, we will discuss the uses of the exec command in shell scripts on Linux, including examples and commands with output.

Uses of the exec command

There are several uses of the exec command in shell scripts on Linux. Some of the main uses are −

Executing a command or a script with exec command

The most basic use of the exec command is to execute a command or a script in place of the current shell. For example, to execute the ls command, you can use the following command-

Example

$ exec ls

This will execute the ls command and exit the current shell. Output −

Example

file1.txt file2.txt file3.txt

Redirecting output with exec command

The exec command can be used to redirect the output of a command or a script to a file or to another command. For example, to redirect the output of the ls command to a file called output.txt, you can use the following command −

Example

$ exec ls > output.txt

This will execute the ls command and redirect the output to the output.txt file. The output is not displayed on the terminal, but it is saved to the output.txt file.

Execute a command in a subshell with exec command

The exec command can be used to execute a command or a script in a subshell. A subshell is a separate shell process that is created to execute a command or a script. To execute a command or a script in a subshell, you can use the ( ) characters around the command or script. For example, to execute the ls command in a subshell, you can use the following command −

Example

$ exec (ls)

This will create a subshell and execute the ls command in it. Output −

Example

file1.txt file2.txt file3.txt

Execute commands with different environment with exec

The exec command can be used to execute a command or a script with a different environment. An environment is a set of variables that can be used by the shell and the commands that it executes. To execute a command or a script with a different environment, you can use the env command followed by the environment variables and the command or script. For example, to execute the ls command with the LANG environment vaiable set to en_US.UTF-8, you can use the following command −

Example

$ exec env LANG=en_US.UTF-8 ls

Execute a shell script in different shell with exec

The exec command can be used to execute a script in a different shell. This is useful when you want to use a different shell for a specific part of your script. To execute a script in a different shell, you can use the exec command followed by the path to the shell and the script. For example, to execute a script called script.sh in the bash shell, you can use the following command −

Example

$ exec bash script.sh

Execute a command with a different user with exec

The exec command can be used to execute a command or a script with a different user. This is useful when you want to run a command or a script as a different user, for example, to run a command or a script as the root user. To execute a command or a script with a different user, you can use the sudo command followed by the exec command and the command or script. For example, to execute the ls command as the root user, you can use the following command −

Example

$ sudo exec ls

Conclusion

In conclusion, the exec command is a powerful and useful tool in shell scripts on Linux. It allows you to execute a command or a script in place of the current shell, which can save system resources and simplify the structure of your script. The exec command can also be used to redirect output, execute a command or a script in a subshell, execute a command or a script with a different environment, execute a script in a different shell, and execute a command or a script with a different user.

Updated on: 19-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements