redirections
posted on by Lee Cattarin
baseline
cat tempest.md
You do look, my son, in a moved sort,
As if you were dismay'd: be cheerful, sir.
Our revels now are ended. These our actors,
As I foretold you, were all spirits and
Are melted into air, into thin air:
And, like the baseless fabric of this vision,
The cloud-capp'd towers, the gorgeous palaces,
The solemn temples, the great globe itself,
Ye all which it inherit, shall dissolve
And, like this insubstantial pageant faded,
Leave not a rack behind. We are such stuff
As dreams are made on, and our little life
Is rounded with a sleep.
direct
grep takes a file or files as an argument.
grep "'d" tempest.md
As if you were dismay'd: be cheerful, sir.
The cloud-capp'd towers, the gorgeous palaces,
redirect
If there’s no files, or if - is passed as an argument, grep reads from standard input. < reads a file into standard input (this is shorthand for 0<, 0 being the file descriptor of stdin).
grep "'d" < tempest.md
# or
grep "'d" - < tempest.md
As if you were dismay'd: be cheerful, sir.
The cloud-capp'd towers, the gorgeous palaces,
re-redirect
<(command) creates a file descriptor for the output of command.
grep "'d" - < <(cat tempest.md)
As if you were dismay'd: be cheerful, sir.
The cloud-capp'd towers, the gorgeous palaces,
(In this case, you could substitute cat tempest.md | grep "'d" - but <(command) is handy for cases where commands don’t take stdin, or need to take in multiple files (such as comm).
re-re-redirect
cat can also take stdin when given - or no arguments.
grep "'d" < <(cat < tempest.md)
# or
grep "'d" < <(cat - < tempest.md)
As if you were dismay'd: be cheerful, sir.
The cloud-capp'd towers, the gorgeous palaces,
conclusion
it’s <s all the way down.
postscript: heredoc
grep "'d" << tempest.md
heredoc> oh are we interactive now
heredoc> what am i 'doing' anyway
heredoc> tempest.md
what am i 'doing' anyway
(<< starts an ephemeral document or heredoc. The first argument given is the delimiter, and is more commonly EOF (end of file).)