← 返回命令列表

Linux command

exec 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Execute

exec [command -with -flags]

Example

exec -c [command -with -flags]

Example

exec -l [command -with -flags]

Example

exec -a [name] [command -with -flags]

Redirect

exec >[path/to/logfile]

Redirect

exec >[path/to/logfile] 2>&1

Example

exec 3<[path/to/file]

Close

exec 3>&-

说明

exec replaces the current shell process with the specified command without creating a child process. When exec completes, the shell session ends because the shell process no longer exists. If no command is specified but redirections are provided, the redirections take effect in the current shell. This allows redirecting stdin, stdout, or stderr for all subsequent commands, and opening or closing arbitrary file descriptors. If the command cannot be executed, a non-interactive shell exits unless the execfail shell option is set. An interactive shell does not exit if exec fails.

参数

-c
Execute the command with a mostly empty environment, clearing inherited environment variables.
-l
Place a dash at the beginning of argv0, causing the command to behave as a login shell.
-a _name_
Pass _name_ as argv0 to the executed command instead of the actual command name.

FAQ

What is the exec command used for?

exec replaces the current shell process with the specified command without creating a child process. When exec completes, the shell session ends because the shell process no longer exists. If no command is specified but redirections are provided, the redirections take effect in the current shell. This allows redirecting stdin, stdout, or stderr for all subsequent commands, and opening or closing arbitrary file descriptors. If the command cannot be executed, a non-interactive shell exits unless the execfail shell option is set. An interactive shell does not exit if exec fails.

How do I run a basic exec example?

Run `exec [command -with -flags]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -c do in exec?

Execute the command with a mostly empty environment, clearing inherited environment variables.