man bash sagt:
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.
Die letzten beiden Zeilen sind wichtig: Wenn Sie execohne Befehl von selbst ausführen , werden die Umleitungen einfach auf die aktuelle Shell angewendet. Sie wissen wahrscheinlich , dass , wenn Sie laufen command > file, der Ausgang commandgeschrieben wird , fileanstatt zu Ihrem Terminal (das eine genannt wird Umleitung ). Wenn Sie exec > filestattdessen ausführen , gilt die Umleitung für die gesamte Shell: Jede Ausgabe, die von der Shell erzeugt wird, filewird auf Ihr Terminal anstatt auf dieses geschrieben . Zum Beispiel hier
bash-3.2$ bash
bash-3.2$ exec > file
bash-3.2$ date
bash-3.2$ exit
bash-3.2$ cat file
Thu 18 Sep 2014 23:56:25 CEST
Ich starte zuerst eine neue bashShell. Dann starte ich in dieser neuen Shell exec > file, damit alle Ausgaben umgeleitet werden file. Zwar laufe ich danach dateaber ich bekomme keine Ausgabe, weil die Ausgabe umgeleitet wird file. Dann beende ich meine Shell (damit die Umleitung nicht mehr gilt) und sehe, dass sie filetatsächlich die Ausgabe des datezuvor ausgeführten Befehls enthält .