-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
pages*: replace invocations of cat
with shell redirection
#16924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This was discussed in the past #13668 The ease of reading from left to right is a very strong reason to use cat for new users. |
Fair point, though I stand by my idea that it is instilling a bad practice. |
How significant is spawning a new process? Can you give rough benchmark numbers? I doubt modern computers mind it at all. |
@@ -9,7 +9,7 @@ | |||
|
|||
- Read JSON from `stdin` and execute a specified JSONPath expression: | |||
|
|||
`cat {{path/to/file.json}} | ajson '{{$..json[?(@.path)]}}'` | |||
`ajson '{{$..json[?(@.path)]}}' < {{path/to/file.json}}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`ajson '{{$..json[?(@.path)]}}' < {{path/to/file.json}}` | |
`ajson < {{path/to/file.json}} '{{$..json[?(@.path)]}}'` |
While this is not a direct approval of the idea, I have an improvement suggestion: Always have the shell redirection come after the program invocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`ajson '{{$..json[?(@.path)]}}' < {{path/to/file.json}}` | |
`ajson <{{path/to/file.json}} '{{$..json[?(@.path)]}}'` |
Also I wonder if having no space in between makes the example more or less clear to the user viewing the example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's not something that can be used universally. ip rule
for example has writing to a file and reading from a file on the same spot at the end of the commans which makes it easy to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, although placing the redirection before the arguments feels confusing imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It don't think it would be confusing. Having the shell redirection point directly at the program has the same effect visually as using cat
where pushing data into stdin
is the first thing that happens.
Can you give some examples to illustrate why this is a bad practice? E.g. is it prone to some kind of error or user mistake, does it obscure something that the user should be aware of? Usually when it's called a bad practice, the only rationale given is vague personal preference or the presumed perf cost of process spawning. |
A very brief opinion from minimal investigation:
As most of these are in the |
Honestly, I wrote a simple a test and the script that used
I approached this from a Unix perspective, and thus I'm not really well versed on Windows, so I guess that's a fair point. Albeit a bit strange that Powershell supports output redirection and pipes but not input redirection. |
The performance penalty exists, though perhaps not that significant. But there's also this. |
When you're writing a shell script, I agree that using However, when you're just using the command-line, it doesn't matter nearly as much. A single use of |
cat
with shell redirectioncat
with shell redirection
I think that we can close this PR then |
In general, there's no need to pipe the output of
cat
into another command just to operate on the contents of a file. A shell redirection serves that exact purpose and does not include the overhead of spawning a new process. Given the fact that one of the goals oftldr
is to ease new users into the world of the command-line, it is best not to instill bad practices on new users.