Skip to content

Commit 5a8b381

Browse files
committed
Add xargs Default Command Is echo as a Unix TIL
1 parent 3270682 commit 5a8b381

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1322 TILs and counting..._
13+
_1323 TILs and counting..._
1414

1515
---
1616

@@ -1343,6 +1343,7 @@ _1322 TILs and counting..._
13431343
- [Watch The Difference](unix/watch-the-difference.md)
13441344
- [Watch This Run Repeatedly](unix/watch-this-run-repeatedly.md)
13451345
- [Where Are The Binaries?](unix/where-are-the-binaries.md)
1346+
- [xargs Default Command Is echo](unix/xargs-default-command-is-echo.md)
13461347

13471348
### Vercel
13481349

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# xargs Default Command Is echo
2+
3+
With the `fd` command, all the found files are output line by line like so.
4+
5+
```bash
6+
❯ fd '.*.md' --max-depth=1
7+
CONTRIBUTING.md
8+
README.md
9+
TODO.md
10+
built-in-commands.md
11+
newsletter.md
12+
tailwind.md
13+
```
14+
15+
I can normalize the whitespace, squishing everything to be separated by single
16+
spaces by piping it to `xargs`. This is equivalent to call `xargs` with `echo`.
17+
That is because `echo` is the default command that `xargs` uses when another
18+
command isn't given.
19+
20+
```bash
21+
❯ fd '.*.md' --max-depth=1 | xargs
22+
CONTRIBUTING.md README.md TODO.md built-in-commands.md newsletter.md tailwind.md
23+
24+
❯ fd '.*.md' --max-depth=1 | xargs echo
25+
CONTRIBUTING.md README.md TODO.md built-in-commands.md newsletter.md tailwind.md
26+
```
27+
28+
We can see further evidence of that by using the `-n` flag with `2` to have it
29+
process results two at a time. In either case, each pair of file names is
30+
echo'd before the next two are processed.
31+
32+
```bash
33+
❯ fd '.*.md' --max-depth=1 | xargs -n2
34+
CONTRIBUTING.md README.md
35+
TODO.md built-in-commands.md
36+
newsletter.md tailwind.md
37+
38+
❯ fd '.*.md' --max-depth=1 | xargs -n2 echo
39+
CONTRIBUTING.md README.md
40+
TODO.md built-in-commands.md
41+
newsletter.md tailwind.md
42+
```
43+
44+
See `man xargs` for more details, as well as this [excellent
45+
article](https://abhinavg.net/2022/06/04/xargs-spaces/).

0 commit comments

Comments
 (0)