Skip to content

Commit 48282c0

Browse files
committed
jq
1 parent 2d3ef31 commit 48282c0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

book.org

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142

143143
=my_script 'a b c' d e=
144144

145+
Do you want a more advanced way to use arrays in shell? use =jq=. [[https://alurm.github.io/blog/2025-08-07-first-class-lists-in-shells.html][Link]]
145146
** Pass Arrays around
146147
#+begin_src bash
147148
a=('Track 01.mp3' 'Track 02.mp3')
@@ -2508,6 +2509,48 @@ The tricks here:
25082509
docker run --rm -ti -v =(echo "OHAI"):/tmp/foo ubuntu cat /tmp/foo
25092510
#+end_src
25102511

2512+
2513+
** =(), the crazy stuff
2514+
The problem to solve here is to
2515+
`echo "A\nB" | { grep 'B' ; grep 'A' }`.
2516+
2517+
With `tee >(grep 'B') >(grep 'A')` it doesn't maintain the order.
2518+
2519+
echo -e "A\nB" | tee >(grep "B") | grep "A" it doesn't maintain order either
2520+
2521+
Another approach is:
2522+
foo() { 0s - [25-03-25 14:52]
2523+
grep 'B' /dev/stdin
2524+
grep A /dev/stdin
2525+
}
2526+
2527+
but the problem there is that stdin gets consumed and the second doesn't get anything.
2528+
2529+
foo() { 0s - [25-03-25 14:52]
2530+
grep 'B' $1
2531+
grep A $1
2532+
}
2533+
and then call it with foo <(echo "A\nB") . but doesn't work, because <() creates a pipe.
2534+
and then call it with foo =(echo "A\nB") . This works. Ok, we have a way.
2535+
2536+
foo() {
2537+
grep 'B' =(cat /dev/stdin)
2538+
grep A =(cat /dev/stdin)
2539+
}
2540+
that still doesn't work, because the moment you do `cat /dev/stdin`, /dev/stdin gets emptied
2541+
2542+
So we can create a function that makes /dev/stdin a file instead of a pipe
2543+
2544+
bar() {
2545+
grep 'B' $1
2546+
grep A $1
2547+
}
2548+
2549+
foo() {
2550+
bar =(cat /dev/stdin)
2551+
}
2552+
2553+
echo "A\nB"| foo . AND IT WORKS NOW
25112554
* TODO patterns
25122555
** just use cat/netcat/pipes with <()
25132556

0 commit comments

Comments
 (0)