$(git ls-files -s | wc -l)
和$(git ls-files -s >out && wc -l <out)
这两个命令是一样的$(git ls-files -s | wc -l)
最终得到错误。
When you pipe the output of one program into the input of another, as in:
$(git ls-files -s | wc -l)
...the programs run concurrently. wc
will start counting lines as soon as it receives them. The pipe also directs the output of git
to the input of wc
without any intermediate file.
Note that in this case, wc
will run even if the git
command fails for some reason, so you'll get the wc
output (in most cases, 0
).
In your second example:
$(git ls-files -s >out && wc -l <out)
...the git
command runs first, and stores its results in a file called out
. Then, if that was successful, wc
runs and counts the lines. Because of &&
, if the git
command fails, wc
won't run at all. In either case, you'll have a file named out
laying around with the results of the git
command in it.
Piping is generally better; it'll run faster and if you don't need to keep the intermediate results, it won't have any side effects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.