linnux exec
执行 COMMAND,用指定程序替换此 shell
exec 作用
- 执行命令
exec
命令用于在当前 shell 中执行一个命令,并替换当前 shell。即 exec 命令会关闭当前 shell,然后启动一个新的 shell 来执行指定的命令- 如
exec rbash
- 设置重定向
- 如
exec >> /tmp/x.log 2>&1
将标准错误 2 重定向到标准输出 1,并输出到 /tmp/x.log
,如果在 shell 文件中,该命令行下面所有的输出都会被输出到 /tmp/x.log
中
help
$ exec --help
exec: exec [-cl] [-a name] [command [argument ...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
Options:
-a name pass NAME as the zeroth argument to COMMAND
-c execute COMMAND with an empty environment
-l place a dash in the zeroth argument to COMMAND
If the command cannot be executed, a non-interactive shell exits, unless
the shell option `execfail' is set.
Exit Status:
Returns success unless COMMAND is not found or a redirection error occurs.