1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00

Add xargs Default Command Is echo as a Unix TIL

This commit is contained in:
jbranchaud
2023-07-07 10:11:29 -05:00
parent 32706827e9
commit 5a8b381de8
2 changed files with 47 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1322 TILs and counting..._ _1323 TILs and counting..._
--- ---
@@ -1343,6 +1343,7 @@ _1322 TILs and counting..._
- [Watch The Difference](unix/watch-the-difference.md) - [Watch The Difference](unix/watch-the-difference.md)
- [Watch This Run Repeatedly](unix/watch-this-run-repeatedly.md) - [Watch This Run Repeatedly](unix/watch-this-run-repeatedly.md)
- [Where Are The Binaries?](unix/where-are-the-binaries.md) - [Where Are The Binaries?](unix/where-are-the-binaries.md)
- [xargs Default Command Is echo](unix/xargs-default-command-is-echo.md)
### Vercel ### Vercel

View File

@@ -0,0 +1,45 @@
# xargs Default Command Is echo
With the `fd` command, all the found files are output line by line like so.
```bash
fd '.*.md' --max-depth=1
CONTRIBUTING.md
README.md
TODO.md
built-in-commands.md
newsletter.md
tailwind.md
```
I can normalize the whitespace, squishing everything to be separated by single
spaces by piping it to `xargs`. This is equivalent to call `xargs` with `echo`.
That is because `echo` is the default command that `xargs` uses when another
command isn't given.
```bash
fd '.*.md' --max-depth=1 | xargs
CONTRIBUTING.md README.md TODO.md built-in-commands.md newsletter.md tailwind.md
fd '.*.md' --max-depth=1 | xargs echo
CONTRIBUTING.md README.md TODO.md built-in-commands.md newsletter.md tailwind.md
```
We can see further evidence of that by using the `-n` flag with `2` to have it
process results two at a time. In either case, each pair of file names is
echo'd before the next two are processed.
```bash
fd '.*.md' --max-depth=1 | xargs -n2
CONTRIBUTING.md README.md
TODO.md built-in-commands.md
newsletter.md tailwind.md
fd '.*.md' --max-depth=1 | xargs -n2 echo
CONTRIBUTING.md README.md
TODO.md built-in-commands.md
newsletter.md tailwind.md
```
See `man xargs` for more details, as well as this [excellent
article](https://abhinavg.net/2022/06/04/xargs-spaces/).