2 added 2 characters in body
source | link

The "how" has been well explained in other answers; I want to address the "why" the OP's code doesn't work.

The keynote is that output redirections are marked before variable expansion. Redirections are actually performed after variable expansion (hence why you can redirect output to a filename which is stored in a variable), but the shell identifies the redirections for later processing before variables are expanded.

In other words, once variables are expanded it is "too late" for a redirection character (< or >, etc.) to be considered, because the shell has already identified which parts of the command string it is going to handle as redirections.

For further reading, see steps 1 and 3 listed under:

LESS='+/^simple^SIMPLE commandCOMMAND expansion'EXPANSION' man bash

The "how" has been well explained in other answers; I want to address the "why" the OP's code doesn't work.

The keynote is that output redirections are marked before variable expansion. Redirections are actually performed after variable expansion (hence why you can redirect output to a filename which is stored in a variable), but the shell identifies the redirections for later processing before variables are expanded.

In other words, once variables are expanded it is "too late" for a redirection character (< or >, etc.) to be considered, because the shell has already identified which parts of the command string it is going to handle as redirections.

For further reading, see steps 1 and 3 listed under:

LESS='+/^simple command expansion' man bash

The "how" has been well explained in other answers; I want to address the "why" the OP's code doesn't work.

The keynote is that output redirections are marked before variable expansion. Redirections are actually performed after variable expansion (hence why you can redirect output to a filename which is stored in a variable), but the shell identifies the redirections for later processing before variables are expanded.

In other words, once variables are expanded it is "too late" for a redirection character (< or >, etc.) to be considered, because the shell has already identified which parts of the command string it is going to handle as redirections.

For further reading, see steps 1 and 3 listed under:

LESS='+/^SIMPLE COMMAND EXPANSION' man bash
1
source | link

The "how" has been well explained in other answers; I want to address the "why" the OP's code doesn't work.

The keynote is that output redirections are marked before variable expansion. Redirections are actually performed after variable expansion (hence why you can redirect output to a filename which is stored in a variable), but the shell identifies the redirections for later processing before variables are expanded.

In other words, once variables are expanded it is "too late" for a redirection character (< or >, etc.) to be considered, because the shell has already identified which parts of the command string it is going to handle as redirections.

For further reading, see steps 1 and 3 listed under:

LESS='+/^simple command expansion' man bash