exec Command in Linux



exec is a Linux command that allows you to replace the current shell process with a specified command, without creating a new process. It means that once the exec runs a command, the original shell does not return.

The exec command is pretty useful in scripts for running a command and then exiting, or changing the current shell environment. The simple case is if you run a command exec ls, it will replace the shell with the ls command and it won’t be possible for you to return to the shell unless you start a new session.

Table of Contents

Here is a comprehensive guide to the options available with the exec command −

Syntax for exec Command in Linux

The basic syntax to use the exec command in Linux is given below −

exec [options] [command [arguments]] [redirection]

Here,

  • [options] are optional flags that modify the behavior of exec.
  • [command [arguments]] is the command to be executed along with any arguments it requires.
  • [redirection] is an optional redirection to change the input/output behavior.

Different Options Available for exec Command

With exec command, there are few limited options you can use for changing the command’s behavior. These option can be found in the table given below −

Option Description
-a name Passes a specified name as the zeroth argument.
-c Executes the command with an empty environment.
-l Passes a dash as the zeroth argument.

Examples of exec Command in Linux

Let’s discuss a few examples of exec command in Linux system −

  • Replace a Shell with a Command
  • Run a Command with Arguments
  • Execute a Script
  • Change Shell
  • Redirect Output
  • Execute with an Empty Environment
  • Replace Shell with Sleep Command

Replace Shell with a Command

One of the basic examples of the exec command on Linux is to replace shell with a command. You can do this by using the exec command followed by the command you want to replace it with the shell. For example, to replace shell with the ls command, follow the below-given command −

exec ls

This replaces the current shell with the ls command, listing directory contents and then exiting the shell.

Run a Command with Arguments

The exec command can also be used to run a command with arguments. For example, to run an echo command with the argument “Hello, World!”, you can use −

exec echo "Hello, World!"

This replaces the current shell with the echo command, printing “Hello, World!” and then exiting.

Execute a Script

Apart from replacing shell with a command, you can use the exec command to execute a script. It can be done by using the exec command followed by the script name you want to execute. For example, to execute a script called myscript.sh, you can use −

exec ./myscript.sh

Change Shell

You can also change your current shell with the help of exec command. For example, to change the shell to Bourne shell (sh), you can use the below-given command −

exec sh
Change Shell

Redirect Output

Another advantage of the exec command is that it allows you to redirect output. For example, to redirect all subsequent output to a file named output.txt, you can use the following command −

exec > output.txt

This command replaces the current shell’s standard output with the file output.txt. Any command run after this will have its output written to output.txt instead of the terminal.

Redirect Output

Execute with an Empty Environment

The exec command can also be used to run a program with an empty environment, which means no environment variables are passed to the new process. For example, to run the env command with an empty environment, you can use the following command −

exec -c env

This command replaces the current shell with the env command, which will show no environment variables because it starts with an empty environment.

Replace Shell with the Sleep Command

If you want to replace the current shell with the sleep command, you can do that easily with the help of exec command. This process will pause the shell for a specified duration. For example, to replace the shell with a command that sleeps for 10 seconds, you can use the below-given command −

exec sleep 10

The above command replaces the current shell process with the sleep command and causes the shell to pause for a duration of 10 seconds. During this time, the shell is effectively inactive, and once the provided duration is over, the shell session ends.

Replace Shell with the Sleep Command

That’s how you can use the exec command on your Linux system.

Conclusion

The exec command is a powerful tool used for replacing the current shell process with a specific command instead of creating a new process. In this article, we have provided the basic syntax of exec command along with its options that can be used with the command. Further, a few examples of exec commands are also provided to help you learn the usage of the command on Linux systems.

By understanding and utilizing the exec command, you can optimize your resource usage and manage processes more efficiently on your Linux system.

Advertisements