man bash
говорит:
exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes command to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non-interactive shell exits, unless the execfail shell option is enabled. In that case, it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
Последние две строки - это то, что важно: если вы запустите exec
сам по себе, без команды, он просто заставит перенаправления применяться к текущей оболочке. Вы, вероятно, знаете, что, когда вы бежите command > file
, выход из command
записывается в file
вместо того, чтобы на ваш терминал (это называется перенаправление). Если вы запустите exec > file
вместо этого перенаправление применяется ко всей оболочке: любой вывод, созданный оболочкой, записывается в file
вместо того, чтобы обращаться к вашему терминалу. Например, здесь
bash-3.2$ bashbash-3.2$ exec > filebash-3.2$ datebash-3.2$ exitbash-3.2$ cat fileThu 18 Sep 2014 23:56:25 CEST
Я впервые начинаю новую bash
ракушка. Затем в этой новой оболочке я запускаю exec > file
, так что все выходные данные перенаправляются на file
. Действительно, после этого я бегу date
но я не получаю никаких выходных данных, потому что выходные данные перенаправляются на file
. Затем я выхожу из своей оболочки (так что перенаправление больше не применяется), и я вижу, что file
действительно содержит выходные данные date
команда, которую я запустил ранее.