exec

最後更新: 2020-08-10

介紹

exec command in Linux is used to execute a command from the bash itself.

This command does not create a new process it just replaces the bash with the command to be executed.

If the exec command is successful, it does not return to the calling process.

 


Usage

 

Syntax

exec [-cl] [-a name] [command [arguments]] [redirection ...]

Options

  • c: It is used to execute the command with empty environment.
  • a name: Used to pass a name as the zeroth argument of the command.
  • l: Used to pass dash as the zeroth argument of the command.

The exec command can be used in two modes:

  • Exec with a command as an argument: exec ls
  • Exec without a command: exec > log.txt          
    # The redirections can be used to modify the current shell environment.

 


Usage - redirect STDIN, STDOUT, STDERR

 

exec 0< '/dev/null'
exec 1> '/dev/null'
exec 2> '/dev/null'