Skip to main content
added 725 characters in body
Source Link
Petr Skocik
  • 29.7k
  • 18
  • 90
  • 155

In shells, exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run tee process that has $LOGFILE as its first argument and then you redirect the stderr descriptor (2) to the same place as where descriptor 1 now points (the tee pipe).

Keeping in mind that filedescriptors get inherited, you've just made all future stdout and stderr output go to the tee process, which writes it to $LOGFILE and to wherever filedescriptor 1 pointed to originally (probably your terminal).


Note: The tee process outputs to the original stdout (=the original filedescriptor 1) because, as you can learn from /searching bash(1) for Simple Command Expansion and Process Substitution, process substitution ( >() <() ) happens (along with other expansions) before redirections get executed, which means that the redirection in exec 1> >(tee "$LOGFILE") happens after tee has started, leaving tee with the same filedescriptor 1 that it inherited from the parent shell. (If it were the other way around, tee would be made to write to its own input, which might make it deadlock, depending on its IO pattern).

In shells, exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run tee process that has $LOGFILE as its first argument and then you redirect the stderr descriptor (2) to the same place as where descriptor 1 now points (the tee pipe).

Keeping in mind that filedescriptors get inherited, you've just made all future stdout and stderr output go to the tee process, which writes it to $LOGFILE and to wherever filedescriptor 1 pointed to originally (probably your terminal).

In shells, exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run tee process that has $LOGFILE as its first argument and then you redirect the stderr descriptor (2) to the same place as where descriptor 1 now points (the tee pipe).

Keeping in mind that filedescriptors get inherited, you've just made all future stdout and stderr output go to the tee process, which writes it to $LOGFILE and to wherever filedescriptor 1 pointed to originally (probably your terminal).


Note: The tee process outputs to the original stdout (=the original filedescriptor 1) because, as you can learn from /searching bash(1) for Simple Command Expansion and Process Substitution, process substitution ( >() <() ) happens (along with other expansions) before redirections get executed, which means that the redirection in exec 1> >(tee "$LOGFILE") happens after tee has started, leaving tee with the same filedescriptor 1 that it inherited from the parent shell. (If it were the other way around, tee would be made to write to its own input, which might make it deadlock, depending on its IO pattern).

added 34 characters in body
Source Link
Petr Skocik
  • 29.7k
  • 18
  • 90
  • 155

In shells, exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdoutstdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run teetee process that has $LOGFILE as its first argument and then you redirect the stderrstderr descriptor (2) to the same place as where 1descriptor 1 now points (the tee pipe).

Keeping in mind that filedescriptors get inherited, you've just made all stdoutfuture stdout and stderrstderr output go to the tee process, which writes it to $LOGFILE and to wherever filedescriptor 1 pointed to originally (which is probablyprobably your terminal).

In shells exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run tee process that has $LOGFILE as its first argument and then you redirect the stderr descriptor (2) to the same place as where 1 points.

Keeping in mind that filedescriptors get inherited, you've just made all stdout and stderr output go to tee which writes it to $LOGFILE and wherever filedescriptor 1 pointed to originally (which is probably your terminal).

In shells, exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run tee process that has $LOGFILE as its first argument and then you redirect the stderr descriptor (2) to the same place as where descriptor 1 now points (the tee pipe).

Keeping in mind that filedescriptors get inherited, you've just made all future stdout and stderr output go to the tee process, which writes it to $LOGFILE and to wherever filedescriptor 1 pointed to originally (probably your terminal).

added 34 characters in body
Source Link
Petr Skocik
  • 29.7k
  • 18
  • 90
  • 155

In shells exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run tee process that has $LOGFILE as its first argument and then you redirect the stderr descriptor (2) to the same place as where 1 points.

Keeping in mind that filedescriptors get inherited, you've just made all stdout and stderr output go to tee which writes it to $LOGFILE and wherever filedescriptor 1 pointed to originally (which is probably your terminal).

In shells exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run tee process that has $LOGFILE as its first argument and then you redirect the stderr descriptor (2) to the same place as where 1 points.

Keeping in mind that filedescriptors get inherited, you've just made all stdout and stderr output go to tee which writes it to $LOGFILE and wherever filedescriptor 1 pointed to originally.

In shells exec does 1) file openings and redirections 2) actual execing (replacing the current process image with another process image).

These execs are redirections.

First you redirect (exec 1> >(tee $LOGFILE)) the stdout descriptor (1) to a process substitution-generated pipe connected to a concurrently run tee process that has $LOGFILE as its first argument and then you redirect the stderr descriptor (2) to the same place as where 1 points.

Keeping in mind that filedescriptors get inherited, you've just made all stdout and stderr output go to tee which writes it to $LOGFILE and wherever filedescriptor 1 pointed to originally (which is probably your terminal).

Source Link
Petr Skocik
  • 29.7k
  • 18
  • 90
  • 155
Loading