From 2ce75a8600ee77dff70d06573db9f5365ebe7218 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 13 Sep 2025 12:05:31 +0300 Subject: Update content for gemtext --- about/resources.gmi | 204 ++--- ...2021-05-16-personal-bash-coding-style-guide.gmi | 1 + ...05-gemtexter-one-bash-script-to-rule-it-all.gmi | 1 + gemfeed/2021-11-29-bash-golf-part-1.gmi | 2 + gemfeed/2022-01-01-bash-golf-part-2.gmi | 2 + ...-static-web-photo-albums-with-photoalbum.sh.gmi | 1 + gemfeed/2023-12-10-bash-golf-part-3.gmi | 2 + .../2024-01-13-one-reason-why-i-love-openbsd.gmi | 2 + .../2024-08-05-typing-127.1-words-per-minute.gmi | 2 +- gemfeed/2025-09-14-bash-golf-part-4.gmi | 536 ++++++++++++ gemfeed/2025-09-14-bash-golf-part-4.gmi.tpl | 510 +++++++++++ gemfeed/atom.xml | 962 ++++++++++++++------- gemfeed/index.gmi | 1 + index.gmi | 3 +- uptime-stats.gmi | 2 +- 15 files changed, 1824 insertions(+), 407 deletions(-) create mode 100644 gemfeed/2025-09-14-bash-golf-part-4.gmi create mode 100644 gemfeed/2025-09-14-bash-golf-part-4.gmi.tpl diff --git a/about/resources.gmi b/about/resources.gmi index 372934f9..46dc02eb 100644 --- a/about/resources.gmi +++ b/about/resources.gmi @@ -35,106 +35,106 @@ You won't find any links on this site because, over time, the links will break. In random order: +* Tmux 2: Productive Mouse-free Development; Brain P. Hogan; The Pragmatic Programmers +* Think Raku (aka Think Perl 6); Laurent Rosenfeld, Allen B. Downey; O'Reilly +* Developing Games in Java; David Brackeen and others...; New Riders +* Hands-on Infrastructure Monitoring with Prometheus; Joel Bastos, Pedro Araujo; Packt +* Concurrency in Go; Katherine Cox-Buday; O'Reilly +* Clusterbau mit Linux-HA; Michael Schwartzkopff; O'Reilly +* Programming Perl aka "The Camel Book"; Tom Christiansen, brian d foy, Larry Wall & Jon Orwant; O'Reilly +* DNS and BIND; Cricket Liu; O'Reilly +* Effective Java; Joshua Bloch; Addison-Wesley Professional +* Perl New Features; Joshua McAdams, brian d foy; Perl School +* Pro Puppet; James Turnbull, Jeffrey McCune; Apress +* The DevOps Handbook; Gene Kim, Jez Humble, Patrick Debois, John Willis; Audible +* C++ Programming Language; Bjarne Stroustrup; +* Distributed Systems: Principles and Paradigms; Andrew S. Tanenbaum; Pearson * The Kubernetes Book; Nigel Poulton; Unabridged Audiobook -* Object-Oriented Programming with ANSI-C; Axel-Tobias Schreiner -* Effective awk programming; Arnold Robbins; O'Reilly +* Raku Fundamentals; Moritz Lenz; Apress * Learn You a Haskell for Great Good!; Miran Lipovaca; No Starch Press -* Raku Recipes; J.J. Merelo; Apress -* Polished Ruby Programming; Jeremy Evans; Packt Publishing +* Amazon Web Services in Action; Michael Wittig and Andreas Wittig; Manning Publications +* Systems Performance Tuning; Gian-Paolo D. Musumeci and others...; O'Reilly +* Ultimate Go Notebook; Bill Kennedy +* The Pragmatic Programmer; David Thomas; Addison-Wesley +* Java ist auch eine Insel; Christian Ullenboom; +* Funktionale Programmierung; Peter Pepper; Springer * Leanring eBPF; Liz Rice; O'Reilly -* Site Reliability Engineering; How Google runs production systems; O'Reilly -* Learn You Some Erlang for Great Good; Fred Herbert; No Starch Press -* Go Brain Teasers - Exercise Your Mind; Miki Tebeka; The Pragmatic Programmers -* Developing Games in Java; David Brackeen and others...; New Riders -* Think Raku (aka Think Perl 6); Laurent Rosenfeld, Allen B. Downey; O'Reilly * Programming Ruby 3.3 (5th Edition); Noel Rappin, with Dave Thomas; The Pragmatic Bookshelf +* Effective awk programming; Arnold Robbins; O'Reilly * Systemprogrammierung in Go; Frank Müller; dpunkt -* The KCNA (Kubernetes and Cloud Native Associate) Book; Nigel Poulton -* The Pragmatic Programmer; David Thomas; Addison-Wesley +* Raku Recipes; J.J. Merelo; Apress +* 97 things every SRE should know; Emil Stolarsky, Jaime Woo; O'Reilly * 100 Go Mistakes and How to Avoid Them; Teiva Harsanyi; Manning Publications -* Java ist auch eine Insel; Christian Ullenboom; -* Funktionale Programmierung; Peter Pepper; Springer -* 21st Century C: C Tips from the New School; Ben Klemens; O'Reilly -* The Go Programming Language; Alan A. A. Donovan; Addison-Wesley Professional -* Clusterbau mit Linux-HA; Michael Schwartzkopff; O'Reilly -* Effective Java; Joshua Bloch; Addison-Wesley Professional +* Object-Oriented Programming with ANSI-C; Axel-Tobias Schreiner +* Higher Order Perl; Mark Dominus; Morgan Kaufmann * Modern Perl; Chromatic ; Onyx Neon Press -* Amazon Web Services in Action; Michael Wittig and Andreas Wittig; Manning Publications -* Pro Puppet; James Turnbull, Jeffrey McCune; Apress -* 97 things every SRE should know; Emil Stolarsky, Jaime Woo; O'Reilly -* Programming Perl aka "The Camel Book"; Tom Christiansen, brian d foy, Larry Wall & Jon Orwant; O'Reilly -* Hands-on Infrastructure Monitoring with Prometheus; Joel Bastos, Pedro Araujo; Packt +* Go Brain Teasers - Exercise Your Mind; Miki Tebeka; The Pragmatic Programmers +* Data Science at the Command Line; Jeroen Janssens; O'Reilly * Kubernetes Cookbook; Sameer Naik, Sébastien Goasguen, Jonathan Michaux; O'Reilly -* Tmux 2: Productive Mouse-free Development; Brain P. Hogan; The Pragmatic Programmers -* DevOps And Site Reliability Engineering Handbook; Stephen Fleming; Audible -* C++ Programming Language; Bjarne Stroustrup; -* Concurrency in Go; Katherine Cox-Buday; O'Reilly +* The Go Programming Language; Alan A. A. Donovan; Addison-Wesley Professional +* The Practise of System and Network Administration; Thomas A. Limoncelli, Christina J. Hogan, Strata R. Chalup; Addison-Wesley Professional Pro Git; Scott Chacon, Ben Straub; Apress * Terraform Cookbook; Mikael Krief; Packt Publishing -* The DevOps Handbook; Gene Kim, Jez Humble, Patrick Debois, John Willis; Audible -* Raku Fundamentals; Moritz Lenz; Apress * The Docker Book; James Turnbull; Kindle -* Ultimate Go Notebook; Bill Kennedy -* Distributed Systems: Principles and Paradigms; Andrew S. Tanenbaum; Pearson -* The Practise of System and Network Administration; Thomas A. Limoncelli, Christina J. Hogan, Strata R. Chalup; Addison-Wesley Professional Pro Git; Scott Chacon, Ben Straub; Apress -* Higher Order Perl; Mark Dominus; Morgan Kaufmann -* Data Science at the Command Line; Jeroen Janssens; O'Reilly -* DNS and BIND; Cricket Liu; O'Reilly -* Systems Performance Tuning; Gian-Paolo D. Musumeci and others...; O'Reilly -* Perl New Features; Joshua McAdams, brian d foy; Perl School +* Site Reliability Engineering; How Google runs production systems; O'Reilly +* 21st Century C: C Tips from the New School; Ben Klemens; O'Reilly +* Polished Ruby Programming; Jeremy Evans; Packt Publishing +* The KCNA (Kubernetes and Cloud Native Associate) Book; Nigel Poulton +* DevOps And Site Reliability Engineering Handbook; Stephen Fleming; Audible +* Learn You Some Erlang for Great Good; Fred Herbert; No Starch Press ## Technical references I didn't read them from the beginning to the end, but I am using them to look up things. The books are in random order: -* Groovy Kurz & Gut; Joerg Staudemeier; O'Reilly -* Relayd and Httpd Mastery; Michael W Lucas -* Algorithms; Robert Sedgewick, Kevin Wayne; Addison Wesley -* Implementing Service Level Objectives; Alex Hidalgo; O'Reilly * The Linux Programming Interface; Michael Kerrisk; No Starch Press -* BPF Performance Tools - Linux System and Application Observability, Brendan Gregg; Addison Wesley -* Understanding the Linux Kernel; Daniel P. Bovet, Marco Cesati; O'Reilly +* Implementing Service Level Objectives; Alex Hidalgo; O'Reilly +* Algorithms; Robert Sedgewick, Kevin Wayne; Addison Wesley * Go: Design Patterns for Real-World Projects; Mat Ryer; Packt +* Groovy Kurz & Gut; Joerg Staudemeier; O'Reilly +* Understanding the Linux Kernel; Daniel P. Bovet, Marco Cesati; O'Reilly +* BPF Performance Tools - Linux System and Application Observability, Brendan Gregg; Addison Wesley +* Relayd and Httpd Mastery; Michael W Lucas ## Self-development and soft-skills books In random order: -* Psycho-Cybernetics; Maxwell Maltz; Perigee Books -* The 7 Habits Of Highly Effective People; Stephen R. Covey; Simon & Schuster UK -* The Off Switch; Mark Cropley; Virgin Books (RE-READ 1ST TIME) * Influence without Authority; A. Cohen, D. Bradford; Wiley -* Getting Things Done; David Allen -* The Obstacle Is The Way; Ryan Holiday; Profile Books Ltd -* The Good Enough Job; Simone Stolzoff; Ebury Edge -* Coders at Work - Reflections on the craft of programming, Peter Seibel and Mitchell Dorian et al., Audiobook -* Buddah and Einstein walk into a Bar; Guy Joseph Ale, Claire Bloom; Blackstone Publishing -* Eat That Frog!; Brian Tracy; Hodder Paperbacks -* Time Management for System Administrators; Thomas A. Limoncelli; O'Reilly -* Solve for Happy; Mo Gawdat (RE-READ 1ST TIME) * The Bullet Journal Method; Ryder Carroll; Fourth Estate -* Consciousness: A Very Short Introduction; Susan Blackmore; Oxford Uiversity Press -* Eat That Frog; Brian Tracy -* Who Moved My Cheese?; Dr. Spencer Johnson; Vermilion -* So Good They Can't Ignore You; Cal Newport; Business Plus -* Digital Minimalism; Cal Newport; Portofolio Penguin -* 101 Essays that change the way you think; Brianna Wiest; Audiobook +* Deep Work; Cal Newport; Piatkus +* Solve for Happy; Mo Gawdat (RE-READ 1ST TIME) * Staff Engineer: Leadership beyond the management track; Will Larson; Audiobook -* Ultralearning; Anna Laurent; Self-published via Amazon -* Ultralearning; Scott Young; Thorsons -* Slow Productivity; Cal Newport; Penguin Random House -* 97 Things Every Engineering Manager Should Know; Camille Fournier; Audiobook * Soft Skills; John Sommez; Manning Publications -* Stop starting, start finishing; Arne Roock; Lean-Kanban University -* The Daily Stoic; Ryan Holiday, Stephen Hanselman; Profile Books -* The Power of Now; Eckhard Tolle; Yellow Kite -* The Complete Software Developer's Career Guide; John Sonmez; Unabridged Audiobook -* The Phoenix Project - A Novel About IT, DevOps, and Helping your Business Win; Gene Kim and Kevin Behr; Trade Select -* Deep Work; Cal Newport; Piatkus +* Eat That Frog!; Brian Tracy; Hodder Paperbacks * Search Inside Yourself - The Unexpected path to Achieving Success, Happiness (and World Peace); Chade-Meng Tan, Daniel Goleman, Jon Kabat-Zinn; HarperOne +* Time Management for System Administrators; Thomas A. Limoncelli; O'Reilly +* The Good Enough Job; Simone Stolzoff; Ebury Edge +* Psycho-Cybernetics; Maxwell Maltz; Perigee Books +* The Obstacle Is The Way; Ryan Holiday; Profile Books Ltd +* Buddah and Einstein walk into a Bar; Guy Joseph Ale, Claire Bloom; Blackstone Publishing +* The Off Switch; Mark Cropley; Virgin Books (RE-READ 1ST TIME) +* Ultralearning; Scott Young; Thorsons +* 101 Essays that change the way you think; Brianna Wiest; Audiobook +* Meditation for Mortals, Oliver Burkeman, Audiobook * Never Split the Difference; Chris Voss, Tahl Raz; Random House Business * The Joy of Missing Out; Christina Crook; New Society Publishers +* 97 Things Every Engineering Manager Should Know; Camille Fournier; Audiobook +* The Daily Stoic; Ryan Holiday, Stephen Hanselman; Profile Books +* Slow Productivity; Cal Newport; Penguin Random House +* The Complete Software Developer's Career Guide; John Sonmez; Unabridged Audiobook +* Stop starting, start finishing; Arne Roock; Lean-Kanban University +* So Good They Can't Ignore You; Cal Newport; Business Plus +* Coders at Work - Reflections on the craft of programming, Peter Seibel and Mitchell Dorian et al., Audiobook +* Who Moved My Cheese?; Dr. Spencer Johnson; Vermilion +* Consciousness: A Very Short Introduction; Susan Blackmore; Oxford Uiversity Press +* Ultralearning; Anna Laurent; Self-published via Amazon +* Digital Minimalism; Cal Newport; Portofolio Penguin * Atomic Habits; James Clear; Random House Business -* Meditation for Mortals, Oliver Burkeman, Audiobook +* The Phoenix Project - A Novel About IT, DevOps, and Helping your Business Win; Gene Kim and Kevin Behr; Trade Select +* Eat That Frog; Brian Tracy +* The Power of Now; Eckhard Tolle; Yellow Kite +* The 7 Habits Of Highly Effective People; Stephen R. Covey; Simon & Schuster UK +* Getting Things Done; David Allen => ../notes/index.gmi Here are notes of mine for some of the books @@ -142,30 +142,30 @@ In random order: Some of these were in-person with exams; others were online learning lectures only. In random order: +* Functional programming lecture; Remote University of Hagen * The Ultimate Kubernetes Bootcamp; School of Devops; O'Reilly Online * Ultimate Go Programming; Bill Kennedy; O'Reilly Online -* Red Hat Certified System Administrator; Course + certification (Although I had the option, I decided not to take the next course as it is more effective to self learn what I need) -* Scripting Vim; Damian Conway; O'Reilly Online +* Apache Tomcat Best Practises; 3-day on-site training +* AWS Immersion Day; Amazon; 1-day interactive online training * Protocol buffers; O'Reilly Online -* Linux Security and Isolation APIs Training; Michael Kerrisk; 3-day on-site training +* Red Hat Certified System Administrator; Course + certification (Although I had the option, I decided not to take the next course as it is more effective to self learn what I need) +* MySQL Deep Dive Workshop; 2-day on-site training * Structure and Interpretation of Computer Programs; Harold Abelson and more...; -* Apache Tomcat Best Practises; 3-day on-site training -* The Well-Grounded Rubyist Video Edition; David. A. Black; O'Reilly Online -* Cloud Operations on AWS - Learn how to configure, deploy, maintain, and troubleshoot your AWS environments; 3-day online live training with labs; Amazon * F5 Loadbalancers Training; 2-day on-site training; F5, Inc. -* Algorithms Video Lectures; Robert Sedgewick; O'Reilly Online -* Functional programming lecture; Remote University of Hagen -* MySQL Deep Dive Workshop; 2-day on-site training -* AWS Immersion Day; Amazon; 1-day interactive online training * Developing IaC with Terraform (with Live Lessons); O'Reilly Online +* Algorithms Video Lectures; Robert Sedgewick; O'Reilly Online +* Cloud Operations on AWS - Learn how to configure, deploy, maintain, and troubleshoot your AWS environments; 3-day online live training with labs; Amazon +* Scripting Vim; Damian Conway; O'Reilly Online +* Linux Security and Isolation APIs Training; Michael Kerrisk; 3-day on-site training +* The Well-Grounded Rubyist Video Edition; David. A. Black; O'Reilly Online ## Technical guides These are not whole books, but guides (smaller or larger) which I found very useful. in random order: -* Raku Guide at https://raku.guide * Advanced Bash-Scripting Guide * How CPUs work at https://cpu.land +* Raku Guide at https://raku.guide ## Podcasts @@ -173,29 +173,29 @@ These are not whole books, but guides (smaller or larger) which I found very use In random order: -* BSD Now [BSD] -* The ProdCast (Google SRE Podcast) -* Deep Questions with Cal Newport -* Backend Banter +* Fallthrough [Golang] * The Pragmatic Engineer Podcast +* Hidden Brain * Fork Around And Find Out -* Modern Mentor -* Maintainable * The Changelog Podcast(s) +* Maintainable +* The ProdCast (Google SRE Podcast) +* BSD Now [BSD] +* Pratical AI * Cup o' Go [Golang] -* Fallthrough [Golang] -* Hidden Brain +* Backend Banter * Dev Interrupted -* Pratical AI +* Modern Mentor +* Deep Questions with Cal Newport ### Podcasts I liked I liked them but am not listening to them anymore. The podcasts have either "finished" (no more episodes) or I stopped listening to them due to time constraints or a shift in my interests. -* FLOSS weekly * Java Pub House -* Ship It (predecessor of Fork Around And Find Out) * Go Time (predecessor of fallthrough) +* FLOSS weekly +* Ship It (predecessor of Fork Around And Find Out) * CRE: Chaosradio Express [german] * Modern Mentor @@ -203,27 +203,27 @@ I liked them but am not listening to them anymore. The podcasts have either "fin This is a mix of tech and non-tech newsletters I am subscribed to. In random order: -* The Imperfectionist -* Andreas Brandhorst Newsletter (Sci-Fi author) -* Changelog News * Applied Go Weekly Newsletter -* Golang Weekly -* byteSizeGo +* VK Newsletter +* Changelog News * The Valuable Dev -* Ruby Weekly +* Golang Weekly * The Pragmatic Engineer -* Monospace Mentor * Register Spill -* VK Newsletter +* Ruby Weekly +* The Imperfectionist +* Monospace Mentor +* byteSizeGo +* Andreas Brandhorst Newsletter (Sci-Fi author) ## Magazines I like(d) This is a mix of tech I like(d). I may not be a current subscriber, but now and then, I buy an issue. In random order: -* freeX (not published anymore) * Linux Magazine -* LWN (online only) * Linux User +* freeX (not published anymore) +* LWN (online only) # Formal education diff --git a/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi b/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi index 66c78be6..5a8ba36c 100644 --- a/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi +++ b/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi @@ -409,6 +409,7 @@ E-Mail your comments to `paul@nospam.buetow.org` :-) Other related posts are: +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 => ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 => ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 diff --git a/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi b/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi index f6ddab74..9548204d 100644 --- a/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi +++ b/gemfeed/2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi @@ -183,6 +183,7 @@ E-Mail your comments to `paul@nospam.buetow.org` :-) Other related posts are: +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 => ./2024-10-02-gemtexter-3.0.0-lets-gemtext-again-4.gmi 2024-10-02 Gemtexter 3.0.0 - Let's Gemtext again⁴ => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 => ./2023-07-21-gemtexter-2.1.0-lets-gemtext-again-3.gmi 2023-07-21 Gemtexter 2.1.0 - Let's Gemtext again³ diff --git a/gemfeed/2021-11-29-bash-golf-part-1.gmi b/gemfeed/2021-11-29-bash-golf-part-1.gmi index 2f0eab35..4b10bcfa 100644 --- a/gemfeed/2021-11-29-bash-golf-part-1.gmi +++ b/gemfeed/2021-11-29-bash-golf-part-1.gmi @@ -7,6 +7,7 @@ This is the first blog post about my Bash Golf series. This series is about rand => ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 (You are currently reading this) => ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 ``` @@ -480,6 +481,7 @@ E-Mail your comments to `paul@nospam.buetow.org` :-) Other related posts are: +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 => ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 => ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 (You are currently reading this) diff --git a/gemfeed/2022-01-01-bash-golf-part-2.gmi b/gemfeed/2022-01-01-bash-golf-part-2.gmi index 5fc23c32..d5c3a91d 100644 --- a/gemfeed/2022-01-01-bash-golf-part-2.gmi +++ b/gemfeed/2022-01-01-bash-golf-part-2.gmi @@ -7,6 +7,7 @@ This is the second blog post about my Bash Golf series. This series is random Ba => ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 => ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 (You are currently reading this) => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 ``` @@ -498,6 +499,7 @@ E-Mail your comments to `paul@nospam.buetow.org` :-) Other related posts are: +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 => ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 (You are currently reading this) => ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 diff --git a/gemfeed/2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.gmi b/gemfeed/2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.gmi index 0bb322e1..90433dc1 100644 --- a/gemfeed/2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.gmi +++ b/gemfeed/2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.gmi @@ -272,6 +272,7 @@ E-Mail your comments to `paul@nospam.buetow.org` :-) Other Bash and KISS-related posts are: +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 => ./2024-04-01-KISS-high-availability-with-OpenBSD.gmi 2024-04-01 KISS high-availability with OpenBSD => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 => ./2023-10-29-kiss-static-web-photo-albums-with-photoalbum.sh.gmi 2023-10-29 KISS static web photo albums with `photoalbum.sh` (You are currently reading this) diff --git a/gemfeed/2023-12-10-bash-golf-part-3.gmi b/gemfeed/2023-12-10-bash-golf-part-3.gmi index b4034bc8..d67ea47e 100644 --- a/gemfeed/2023-12-10-bash-golf-part-3.gmi +++ b/gemfeed/2023-12-10-bash-golf-part-3.gmi @@ -7,6 +7,7 @@ This is the third blog post about my Bash Golf series. This series is random Bas => ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 => ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 (You are currently reading this) +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 ``` @@ -371,6 +372,7 @@ E-Mail your comments to `paul@nospam.buetow.org` :-) Other related posts are: +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 => ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 (You are currently reading this) => ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 => ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 diff --git a/gemfeed/2024-01-13-one-reason-why-i-love-openbsd.gmi b/gemfeed/2024-01-13-one-reason-why-i-love-openbsd.gmi index 01cfe726..7b3759b7 100644 --- a/gemfeed/2024-01-13-one-reason-why-i-love-openbsd.gmi +++ b/gemfeed/2024-01-13-one-reason-why-i-love-openbsd.gmi @@ -29,6 +29,8 @@ $ doas sysupgrade # Update all binaries (including Kernel) `sysupgrade` downloaded and upgraded to the next release and rebooted the system. After the reboot, I run: +> Note to myself: I have to undo the `/var/www` symlink before upgrading, and re-establishing the symlink afterwards again. This is due to disk space constraings on my setup! + ```shell $ doas sysmerge # Update system configuration files $ doas pkg_add -u # Update all packages diff --git a/gemfeed/2024-08-05-typing-127.1-words-per-minute.gmi b/gemfeed/2024-08-05-typing-127.1-words-per-minute.gmi index 4d9ad4e6..bb860da1 100644 --- a/gemfeed/2024-08-05-typing-127.1-words-per-minute.gmi +++ b/gemfeed/2024-08-05-typing-127.1-words-per-minute.gmi @@ -213,7 +213,7 @@ Interested in the Glove80? I suggest also reading this review: As I mentioned, keyboards will remain an expensive hobby of mine. I don't regret anything here, though. After all, I use keyboards at my day job. I've ordered a Kinesis custom build with the Gateron Kangaroo switches, and I'm excited to see how that compares to my current setup. I'm still deciding whether to keep my Gateron Brown-equipped Kinesis as a secondary keyboard or possibly leave it at my in-laws for use when visiting or to sell it. -> Update 2025-02-22: I've received my custom Kinesis Adv. 360 build with the Gateron Baby Kangaroo key switches. I am absolutely in love! I will keep my Gateron Brown versin around, though. +> Update 2025-02-22: I've received my custom Kinesis Adv. 360 build with the Gateron Baby Kangaroo key switches. I am absolutely in love! I will keep my Gateron Brown version around, though. ## Conclusion diff --git a/gemfeed/2025-09-14-bash-golf-part-4.gmi b/gemfeed/2025-09-14-bash-golf-part-4.gmi new file mode 100644 index 00000000..2331e53d --- /dev/null +++ b/gemfeed/2025-09-14-bash-golf-part-4.gmi @@ -0,0 +1,536 @@ +# Bash Golf Part 4 + +> Published at 2025-09-13T12:04:03+03:00 + +This is the fourth blog post about my Bash Golf series. This series is random Bash tips, tricks, and weirdnesses I have encountered over time. + +=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 +=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 +=> ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 (You are currently reading this) + +``` + + '\ '\ '\ '\ . . |>18>> + \ \ \ \ . ' . | + O>> O>> O>> O>> . 'o | + \ .\. .. .\. .. .\. .. . | + /\ . /\ . /\ . /\ . . | + / / . / / .'. / / .'. / / .' . | +jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + Art by Joan Stark, mod. by Paul Buetow +``` + +## Table of Contents + +* ⇢ Bash Golf Part 4 +* ⇢ ⇢ Split pipelines with tee + process substitution +* ⇢ ⇢ Heredocs for remote sessions (and their gotchas) +* ⇢ ⇢ Namespacing and dynamic dispatch with `::` +* ⇢ ⇢ Indirect references with namerefs +* ⇢ ⇢ Function declaration forms +* ⇢ ⇢ Chaining function calls in conditionals +* ⇢ ⇢ Grep, sed, awk quickies +* ⇢ ⇢ Safe xargs with NULs +* ⇢ ⇢ Efficient file-to-variable and arrays +* ⇢ ⇢ Quick password generator +* ⇢ ⇢ `yes` for automation +* ⇢ ⇢ Forcing `true` to fail (and vice versa) +* ⇢ ⇢ Restricted Bash +* ⇢ ⇢ Useless use of cat (and when it’s ok) +* ⇢ ⇢ Atomic locking with `mkdir` +* ⇢ ⇢ Smarter globs and faster find-exec + +## Split pipelines with tee + process substitution + +Sometimes you want to fan out one stream to multiple consumers and still continue the original pipeline. `tee` plus process substitution does exactly that: + +``` +somecommand \ + | tee >(command1) >(command2) \ + | command3 +``` + +All of `command1`, `command2`, and `command3` see the output of `somecommand`. Example: + +```bash +printf 'a\nb\n' \ + | tee >(sed 's/.*/X:&/; s/$/ :c1/') >(tr a-z A-Z | sed 's/$/ :c2/') \ + | sed 's/$/ :c3/' +``` + +Output: + +``` +a :c3 +b :c3 +A :c2 :c3 +B :c2 :c3 +X:a :c1 :c3 +X:b :c1 :c3 +``` + +This relies on Bash process substitution (`>(...)`). Make sure your shell is Bash and not a POSIX `/bin/sh`. + +Example (fails under `dash`/POSIX sh): + +```bash +/bin/sh -c 'echo hi | tee >(cat)' +# /bin/sh: 1: Syntax error: "(" unexpected +``` + +Combine with `set -o pipefail` if failures in side branches should fail the whole pipeline. + +Example: + +```bash +set -o pipefail +printf 'ok\n' | tee >(false) | cat >/dev/null +echo $? # 1 because a side branch failed +``` + +Further reading: + +=> https://blogtitle.github.io/splitting-pipelines/ Splitting pipelines with tee + +## Heredocs for remote sessions (and their gotchas) + +Heredocs are great to send multiple commands over SSH in a readable way: + +```bash +ssh "$SSH_USER@$SSH_HOST" < script.sh + #!/usr/bin/env bash + echo "tab-indented content is dedented" +EOF +``` + +Further reading: + +=> https://rednafi.com/misc/heredoc_headache/ Heredoc headaches and fixes + +## Namespacing and dynamic dispatch with `::` + +You can emulate simple namespacing by encoding hierarchy in function names. One neat pattern is pseudo-inheritance via a tiny `super` helper that maps `pkg::lang::action` to a `pkg::base::action` default. + +```bash +#!/usr/bin/env bash +set -euo pipefail + +super() { + local -r fn=${FUNCNAME[1]} + # Split name on :: and dispatch to base implementation + local -a parts=( ${fn//::/ } ) + "${parts[0]}::base::${parts[2]}" "$@" +} + +foo::base::greet() { echo "base: $@"; } +foo::german::greet() { super "Guten Tag, $@!"; } +foo::english::greet() { super "Good day, $@!"; } + +for lang in german english; do + foo::$lang::greet Paul +done +``` + +Output: + +``` +base: Guten Tag, Paul! +base: Good day, Paul! +``` + +## Indirect references with namerefs + +`declare -n` creates a name reference — a variable that points to another variable. It’s cleaner than `eval` for indirection: + +```bash +user_name=paul +declare -n ref=user_name +echo "$ref" # paul +ref=julia +echo "$user_name" # julia +``` + +Output: + +``` +paul +julia +``` + +Namerefs are local to functions when declared with `local -n`. Requires Bash ≥4.3. + +You can also construct the target name dynamically: + +```bash +make_var() { + local idx=$1; shift + local name="slot_$idx" + printf -v "$name" '%s' "$*" # create variable slot_$idx +} + +get_var() { + local idx=$1 + local -n ref="slot_$idx" # bind ref to slot_$idx + printf '%s\n' "$ref" +} + +make_var 7 "seven" +get_var 7 +``` + +Output: + +``` +seven +``` + +## Function declaration forms + +All of these work in Bash, but only the first one is POSIX-ish: + +```bash +foo() { echo foo; } +function foo { echo foo; } +function foo() { echo foo; } +``` + +Recommendation: prefer `name() { ... }` for portability and consistency. + +## Chaining function calls in conditionals + +Functions return a status like commands. You can short-circuit them in conditionals: + +```bash +deploy_check() { test -f deploy.yaml; } +smoke_test() { curl -fsS http://localhost/healthz >/dev/null; } + +if deploy_check || smoke_test; then + echo "All good." +else + echo "Something failed." >&2 +fi +``` + +You can also compress it golf-style: + +```bash +deploy_check || smoke_test && echo ok || echo fail >&2 +``` + +## Grep, sed, awk quickies + +Word match and context: `grep -w word file`; with context: `grep -C3 foo file` (same as `-A3 -B3`). Example: + +```bash +cat > /tmp/ctx.txt < /tmp/golf/src/a.txt +printf 'bar\n' > /tmp/golf/foo/skip.txt +grep -R --exclude-dir=foo 'bar' /tmp/golf +``` + +Output: + +``` +/tmp/golf/src/a.txt:bar +``` + +Insert lines with sed: `sed -e '1isomething' -e '3isomething' file`. Example: + +```bash +printf 'A\nB\nC\n' > /tmp/s.txt +sed -e '1iHEAD' -e '3iMID' /tmp/s.txt +``` + +Output: + +``` +HEAD +A +B +MID +C +``` + +Drop last column with awk: `awk 'NF{NF-=1};1' file`. Example: + +```bash +printf 'a b c\nx y z\n' > /tmp/t.txt +cat /tmp/t.txt +echo +awk 'NF{NF-=1};1' /tmp/t.txt +``` + +Output: + +``` +a b c +x y z + +a b +x y +``` + +## Safe xargs with NULs + +Avoid breaking on spaces/newlines by pairing `find -print0` with `xargs -0`: + +```bash +find . -type f -name '*.log' -print0 | xargs -0 rm -f +``` + +Example with spaces and NULs only: + +```bash +printf 'a\0b c\0' | xargs -0 -I{} printf '<%s>\n' {} +``` + +Output: + +``` + + +``` + +## Efficient file-to-variable and arrays + +Read a whole file into a variable without spawning `cat`: + +```bash +cfg=$( https://blog.robertelder.org/force-true-command-to-return-false/ Force true to return false + +## Restricted Bash + +`bash -r` (or `rbash`) starts a restricted shell that limits potentially dangerous actions, for example: + +* Changing directories (`cd`). +* Modifying `PATH`, `SHELL`, `BASH_ENV`, or `ENV`. +* Redirecting output. +* Running commands with `/` in the name. +* Using `exec`. + +It’s a coarse sandbox for highly constrained shells; read `man bash` (RESTRICTED SHELL) for details and caveats. + +Example session: + +```bash +rbash -c 'cd /' # cd: restricted +rbash -c 'PATH=/tmp' # PATH: restricted +rbash -c 'echo hi > out' # redirection: restricted +rbash -c '/bin/echo hi' # commands with /: restricted +rbash -c 'exec ls' # exec: restricted +``` + +## Useless use of cat (and when it’s ok) + +Avoid the extra process if a command already reads files or `STDIN`: + +```bash +# Prefer +grep -i foo file +/dev/null; then + trap 'rmdir "$lockdir"' EXIT INT TERM + # critical section + do_work +else + echo "Another instance is running" >&2 + exit 1 +fi +``` + +This works well on Linux. Remove the lock in `trap` so crashes don’t leave stale locks. + +## Smarter globs and faster find-exec + +* Enable extended globs when useful: `shopt -s extglob`; then patterns like `!(tmp|cache)` work. +* Use `-exec ... {} +` to batch many paths in fewer process invocations: + +```bash +find . -name '*.log' -exec gzip -9 {} + +``` + +Example for extglob (exclude two dirs from listing): + +```bash +shopt -s extglob +ls -d -- !(.git|node_modules) 2>/dev/null +``` + +E-Mail your comments to `paul@nospam.buetow.org` :-) + +Other related posts are: + +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 Bash Golf Part 4 (You are currently reading this) +=> ./2023-12-10-bash-golf-part-3.gmi 2023-12-10 Bash Golf Part 3 +=> ./2022-01-01-bash-golf-part-2.gmi 2022-01-01 Bash Golf Part 2 +=> ./2021-11-29-bash-golf-part-1.gmi 2021-11-29 Bash Golf Part 1 +=> ./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.gmi 2021-06-05 Gemtexter - One Bash script to rule it all +=> ./2021-05-16-personal-bash-coding-style-guide.gmi 2021-05-16 Personal Bash coding style guide + +=> ../ Back to the main site diff --git a/gemfeed/2025-09-14-bash-golf-part-4.gmi.tpl b/gemfeed/2025-09-14-bash-golf-part-4.gmi.tpl new file mode 100644 index 00000000..4449b6a2 --- /dev/null +++ b/gemfeed/2025-09-14-bash-golf-part-4.gmi.tpl @@ -0,0 +1,510 @@ +# Bash Golf Part 4 + +> Published at 2025-09-13T12:04:03+03:00 + +This is the fourth blog post about my Bash Golf series. This series is random Bash tips, tricks, and weirdnesses I have encountered over time. + +<< template::inline::index bash-golf + +``` + + '\ '\ '\ '\ . . |>18>> + \ \ \ \ . ' . | + O>> O>> O>> O>> . 'o | + \ .\. .. .\. .. .\. .. . | + /\ . /\ . /\ . /\ . . | + / / . / / .'. / / .'. / / .' . | +jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + Art by Joan Stark, mod. by Paul Buetow +``` + +<< template::inline::toc + +## Split pipelines with tee + process substitution + +Sometimes you want to fan out one stream to multiple consumers and still continue the original pipeline. `tee` plus process substitution does exactly that: + +``` +somecommand \ + | tee >(command1) >(command2) \ + | command3 +``` + +All of `command1`, `command2`, and `command3` see the output of `somecommand`. Example: + +```bash +printf 'a\nb\n' \ + | tee >(sed 's/.*/X:&/; s/$/ :c1/') >(tr a-z A-Z | sed 's/$/ :c2/') \ + | sed 's/$/ :c3/' +``` + +Output: + +``` +a :c3 +b :c3 +A :c2 :c3 +B :c2 :c3 +X:a :c1 :c3 +X:b :c1 :c3 +``` + +This relies on Bash process substitution (`>(...)`). Make sure your shell is Bash and not a POSIX `/bin/sh`. + +Example (fails under `dash`/POSIX sh): + +```bash +/bin/sh -c 'echo hi | tee >(cat)' +# /bin/sh: 1: Syntax error: "(" unexpected +``` + +Combine with `set -o pipefail` if failures in side branches should fail the whole pipeline. + +Example: + +```bash +set -o pipefail +printf 'ok\n' | tee >(false) | cat >/dev/null +echo $? # 1 because a side branch failed +``` + +Further reading: + +=> https://blogtitle.github.io/splitting-pipelines/ Splitting pipelines with tee + +## Heredocs for remote sessions (and their gotchas) + +Heredocs are great to send multiple commands over SSH in a readable way: + +```bash +ssh "$SSH_USER@$SSH_HOST" < script.sh + #!/usr/bin/env bash + echo "tab-indented content is dedented" +EOF +``` + +Further reading: + +=> https://rednafi.com/misc/heredoc_headache/ Heredoc headaches and fixes + +## Namespacing and dynamic dispatch with `::` + +You can emulate simple namespacing by encoding hierarchy in function names. One neat pattern is pseudo-inheritance via a tiny `super` helper that maps `pkg::lang::action` to a `pkg::base::action` default. + +```bash +#!/usr/bin/env bash +set -euo pipefail + +super() { + local -r fn=${FUNCNAME[1]} + # Split name on :: and dispatch to base implementation + local -a parts=( ${fn//::/ } ) + "${parts[0]}::base::${parts[2]}" "$@" +} + +foo::base::greet() { echo "base: $@"; } +foo::german::greet() { super "Guten Tag, $@!"; } +foo::english::greet() { super "Good day, $@!"; } + +for lang in german english; do + foo::$lang::greet Paul +done +``` + +Output: + +``` +base: Guten Tag, Paul! +base: Good day, Paul! +``` + +## Indirect references with namerefs + +`declare -n` creates a name reference — a variable that points to another variable. It’s cleaner than `eval` for indirection: + +```bash +user_name=paul +declare -n ref=user_name +echo "$ref" # paul +ref=julia +echo "$user_name" # julia +``` + +Output: + +``` +paul +julia +``` + +Namerefs are local to functions when declared with `local -n`. Requires Bash ≥4.3. + +You can also construct the target name dynamically: + +```bash +make_var() { + local idx=$1; shift + local name="slot_$idx" + printf -v "$name" '%s' "$*" # create variable slot_$idx +} + +get_var() { + local idx=$1 + local -n ref="slot_$idx" # bind ref to slot_$idx + printf '%s\n' "$ref" +} + +make_var 7 "seven" +get_var 7 +``` + +Output: + +``` +seven +``` + +## Function declaration forms + +All of these work in Bash, but only the first one is POSIX-ish: + +```bash +foo() { echo foo; } +function foo { echo foo; } +function foo() { echo foo; } +``` + +Recommendation: prefer `name() { ... }` for portability and consistency. + +## Chaining function calls in conditionals + +Functions return a status like commands. You can short-circuit them in conditionals: + +```bash +deploy_check() { test -f deploy.yaml; } +smoke_test() { curl -fsS http://localhost/healthz >/dev/null; } + +if deploy_check || smoke_test; then + echo "All good." +else + echo "Something failed." >&2 +fi +``` + +You can also compress it golf-style: + +```bash +deploy_check || smoke_test && echo ok || echo fail >&2 +``` + +## Grep, sed, awk quickies + +Word match and context: `grep -w word file`; with context: `grep -C3 foo file` (same as `-A3 -B3`). Example: + +```bash +cat > /tmp/ctx.txt < /tmp/golf/src/a.txt +printf 'bar\n' > /tmp/golf/foo/skip.txt +grep -R --exclude-dir=foo 'bar' /tmp/golf +``` + +Output: + +``` +/tmp/golf/src/a.txt:bar +``` + +Insert lines with sed: `sed -e '1isomething' -e '3isomething' file`. Example: + +```bash +printf 'A\nB\nC\n' > /tmp/s.txt +sed -e '1iHEAD' -e '3iMID' /tmp/s.txt +``` + +Output: + +``` +HEAD +A +B +MID +C +``` + +Drop last column with awk: `awk 'NF{NF-=1};1' file`. Example: + +```bash +printf 'a b c\nx y z\n' > /tmp/t.txt +cat /tmp/t.txt +echo +awk 'NF{NF-=1};1' /tmp/t.txt +``` + +Output: + +``` +a b c +x y z + +a b +x y +``` + +## Safe xargs with NULs + +Avoid breaking on spaces/newlines by pairing `find -print0` with `xargs -0`: + +```bash +find . -type f -name '*.log' -print0 | xargs -0 rm -f +``` + +Example with spaces and NULs only: + +```bash +printf 'a\0b c\0' | xargs -0 -I{} printf '<%s>\n' {} +``` + +Output: + +``` + + +``` + +## Efficient file-to-variable and arrays + +Read a whole file into a variable without spawning `cat`: + +```bash +cfg=$( https://blog.robertelder.org/force-true-command-to-return-false/ Force true to return false + +## Restricted Bash + +`bash -r` (or `rbash`) starts a restricted shell that limits potentially dangerous actions, for example: + +* Changing directories (`cd`). +* Modifying `PATH`, `SHELL`, `BASH_ENV`, or `ENV`. +* Redirecting output. +* Running commands with `/` in the name. +* Using `exec`. + +It’s a coarse sandbox for highly constrained shells; read `man bash` (RESTRICTED SHELL) for details and caveats. + +Example session: + +```bash +rbash -c 'cd /' # cd: restricted +rbash -c 'PATH=/tmp' # PATH: restricted +rbash -c 'echo hi > out' # redirection: restricted +rbash -c '/bin/echo hi' # commands with /: restricted +rbash -c 'exec ls' # exec: restricted +``` + +## Useless use of cat (and when it’s ok) + +Avoid the extra process if a command already reads files or `STDIN`: + +```bash +# Prefer +grep -i foo file +/dev/null; then + trap 'rmdir "$lockdir"' EXIT INT TERM + # critical section + do_work +else + echo "Another instance is running" >&2 + exit 1 +fi +``` + +This works well on Linux. Remove the lock in `trap` so crashes don’t leave stale locks. + +## Smarter globs and faster find-exec + +* Enable extended globs when useful: `shopt -s extglob`; then patterns like `!(tmp|cache)` work. +* Use `-exec ... {} +` to batch many paths in fewer process invocations: + +```bash +find . -name '*.log' -exec gzip -9 {} + +``` + +Example for extglob (exclude two dirs from listing): + +```bash +shopt -s extglob +ls -d -- !(.git|node_modules) 2>/dev/null +``` + +E-Mail your comments to `paul@nospam.buetow.org` :-) + +Other related posts are: + +<< template::inline::rindex bash + +=> ../ Back to the main site diff --git a/gemfeed/atom.xml b/gemfeed/atom.xml index a7d7dae0..b4c25a4a 100644 --- a/gemfeed/atom.xml +++ b/gemfeed/atom.xml @@ -1,11 +1,664 @@ - 2025-09-11T11:09:24+03:00 + 2025-09-13T12:04:03+03:00 foo.zone feed To be in the .zone! gemini://foo.zone/ + + Bash Golf Part 4 + + gemini://foo.zone/gemfeed/2025-09-14-bash-golf-part-4.gmi + 2025-09-13T12:04:03+03:00 + + Paul Buetow aka snonux + paul@dev.buetow.org + + This is the fourth blog post about my Bash Golf series. This series is random Bash tips, tricks, and weirdnesses I have encountered over time. + + + + Random Weird Things - Part Ⅲ @@ -8667,7 +9320,7 @@ jgs \\`_..---.Y.---.._`//
As I mentioned, keyboards will remain an expensive hobby of mine. I don't regret anything here, though. After all, I use keyboards at my day job. I've ordered a Kinesis custom build with the Gateron Kangaroo switches, and I'm excited to see how that compares to my current setup. I'm still deciding whether to keep my Gateron Brown-equipped Kinesis as a secondary keyboard or possibly leave it at my in-laws for use when visiting or to sell it.

-Update 2025-02-22: I've received my custom Kinesis Adv. 360 build with the Gateron Baby Kangaroo key switches. I am absolutely in love! I will keep my Gateron Brown versin around, though.
+Update 2025-02-22: I've received my custom Kinesis Adv. 360 build with the Gateron Baby Kangaroo key switches. I am absolutely in love! I will keep my Gateron Brown version around, though.

Conclusion



@@ -10792,6 +11445,8 @@ $ doas sysupgrade # Update all binaries (including Kerne
sysupgrade downloaded and upgraded to the next release and rebooted the system. After the reboot, I run:

+Note to myself: I have to undo the /var/www symlink before upgrading, and re-establishing the symlink afterwards again. This is due to disk space constraings on my setup!
+
2023-04-01 "Never split the difference" book notes
2023-03-16 "The Pragmatic Programmer" book notes

-Back to the main site
- - -
- - KISS server monitoring with Gogios - - gemini://foo.zone/gemfeed/2023-06-01-kiss-server-monitoring-with-gogios.gmi - 2023-06-01T21:10:17+03:00 - - Paul Buetow aka snonux - paul@dev.buetow.org - - Gogios is a minimalistic and easy-to-use monitoring tool I programmed in Google Go designed specifically for small-scale self-hosted servers and virtual machines. The primary purpose of Gogios is to monitor my personal server infrastructure for `foo.zone`, my MTAs, my authoritative DNS servers, my NextCloud, Wallabag and Anki sync server installations, etc. - -
-

KISS server monitoring with Gogios


-
-Published at 2023-06-01T21:10:17+03:00
-
-Gogios is a minimalistic and easy-to-use monitoring tool I programmed in Google Go designed specifically for small-scale self-hosted servers and virtual machines. The primary purpose of Gogios is to monitor my personal server infrastructure for foo.zone, my MTAs, my authoritative DNS servers, my NextCloud, Wallabag and Anki sync server installations, etc.
-
-With compatibility with the Nagios Check API, Gogios offers a simple yet effective solution to monitor a limited number of resources. In theory, Gogios scales to a couple of thousand checks, though. You can clone it from Codeberg here:
-
-https://codeberg.org/snonux/gogios
-
-Gogios logo
-
-

Table of Contents


-
-
-
-    _____________________________    ____________________________
-   /                             \  /                            \
-  |    _______________________    ||    ______________________    |
-  |   /                       \   ||   /                      \   |
-  |   | # Alerts with status c|   ||   | # Unhandled alerts:  |   |
-  |   | hanged:               |   ||   |                      |   |
-  |   |                       |   ||   | CRITICAL: Check Pizza|   |
-  |   | OK->CRITICAL: Check Pi|   ||   | : Late delivery      |   |
-  |   | zza: Late delivery    |   ||   |                      |   |
-  |   |                       |   ||   | WARNING: Check Thirst|   |
-  |   |                       |   ||   | : OutofKombuchaExcept|   |
-  |   \_______________________/   ||   \______________________/   |
-  |  /|\ GOGIOS MONITOR 1    _    ||  /|\ GOGIOS MONITOR 2   _    |
-   \_____________________________/  \____________________________/
-     !_________________________!      !________________________!
-
-------------------------------------------------
-ASCII art was modified by Paul Buetow
-The original can be found at
-https://asciiart.website/index.php?art=objects/computers
-
-
-

Motivation


-
-With experience in monitoring solutions like Nagios, Icinga, Prometheus and OpsGenie, these tools often came with many features that I didn't necessarily need for personal use. Contact groups, host groups, check clustering, and the requirement of operating a DBMS and a WebUI added complexity and bloat to my monitoring setup.
-
-My primary goal was to have a single email address for notifications and a simple mechanism to periodically execute standard Nagios check scripts and notify me of any state changes. I wanted the most minimalistic monitoring solution possible but wasn't satisfied with the available options.
-
-This led me to create Gogios, a lightweight monitoring tool tailored to my specific needs. I chose the Go programming language for this project as it comes, in my opinion, with the best balance of ease to use and performance.
-
-

Features


-
-
    -
  • Compatible with Nagios Check scripts: Gogios leverages the widely-used Nagios Check API, allowing to use existing Nagios plugins.
  • -
  • Lightweight and Minimalistic: Gogios is designed to be simple and fairly easy to set up.
  • -
  • Configurable Check Timeout and Concurrency: Gogios allows you to set a timeout for checks and configure the number of concurrent checks, offering flexibility in monitoring your resources.
  • -
  • Configurable check dependency: A check can depend on another check, which enables scenarios like not executing an HTTP check when the server isn't pingable.
  • -
  • Retries: Check retry and retry intervals are configurable per check.
  • -
  • Email Notifications: Gogios can send email notifications regarding the status of monitored services, ensuring you stay informed about potential issues.
  • -
  • CRON-based Execution: Gogios can be quickly scheduled to run periodically via CRON, allowing you to automate monitoring without needing a complex setup.
  • -

-

Example alert


-
-This is an example alert report received via E-Mail. Whereas, [C:2 W:0 U:0 OK:51] means that we've got two alerts in status critical, 0 warnings, 0 unknowns and 51 OKs.
-
-
-Subject: GOGIOS Report [C:2 W:0 U:0 OK:51]
-
-This is the recent Gogios report!
-
-# Alerts with status changed:
-
-OK->CRITICAL: Check ICMP4 vulcan.buetow.org: Check command timed out
-OK->CRITICAL: Check ICMP6 vulcan.buetow.org: Check command timed out
-
-# Unhandled alerts:
-
-CRITICAL: Check ICMP4 vulcan.buetow.org: Check command timed out
-CRITICAL: Check ICMP6 vulcan.buetow.org: Check command timed out
-
-Have a nice day!
-
-
-

Installation


-
-

Compiling and installing Gogios


-
-This document is primarily written for OpenBSD, but applying the corresponding steps to any Unix-like (e.g. Linux-based) operating system should be easy. On systems other than OpenBSD, you may always have to replace does with the sudo command and replace the /usr/local/bin path with /usr/bin.
-
-To compile and install Gogios on OpenBSD, follow these steps:
-
- -
git clone https://codeberg.org/snonux/gogios.git
-cd gogios
-go build -o gogios cmd/gogios/main.go
-doas cp gogios /usr/local/bin/gogios
-doas chmod 755 /usr/local/bin/gogios
-
-
-You can use cross-compilation if you want to compile Gogios for OpenBSD on a Linux system without installing the Go compiler on OpenBSD. Follow these steps:
-
- -
export GOOS=openbsd
-export GOARCH=amd64
-go build -o gogios cmd/gogios/main.go
-
-
-On your OpenBSD system, copy the binary to /usr/local/bin/gogios and set the correct permissions as described in the previous section. All steps described here you could automate with your configuration management system of choice. I use Rexify, the friendly configuration management system, to automate the installation, but that is out of the scope of this document.
-
-https://www.rexify.org
-
-

Setting up user, group and directories


-
-It is best to create a dedicated system user and group for Gogios to ensure proper isolation and security. Here are the steps to create the _gogios user and group under OpenBSD:
-
- -
doas adduser -group _gogios -batch _gogios
-doas usermod -d /var/run/gogios _gogios
-doas mkdir -p /var/run/gogios
-doas chown _gogios:_gogios /var/run/gogios
-doas chmod 750 /var/run/gogios
-
-
-Please note that creating a user and group might differ depending on your operating system. For other operating systems, consult their documentation for creating system users and groups.
-
-

Installing monitoring plugins


-
-Gogios relies on external Nagios or Icinga monitoring plugin scripts. On OpenBSD, you can install the monitoring-plugins package with Gogios. The monitoring-plugins package is a collection of monitoring plugins, similar to Nagios plugins, that can be used to monitor various services and resources:
-
- -
doas pkg_add monitoring-plugins
-doas pkg_add nrpe # If you want to execute checks remotely via NRPE.
-
-
-Once the installation is complete, you can find the monitoring plugins in the /usr/local/libexec/nagios directory, which then can be configured to be used in gogios.json.
-
-

Configuration


-
-

MTA


-
-Gogios requires a local Mail Transfer Agent (MTA) such as Postfix or OpenBSD SMTPD running on the same server where the CRON job (see about the CRON job further below) is executed. The local MTA handles email delivery, allowing Gogios to send email notifications to monitor status changes. Before using Gogios, ensure that you have a properly configured MTA installed and running on your server to facilitate the sending of emails. Once the MTA is set up and functioning correctly, Gogios can leverage it to send email notifications.
-
-You can use the mail command to send an email via the command line on OpenBSD. Here's an example of how to send a test email to ensure that your email server is working correctly:
-
-
-echo 'This is a test email from OpenBSD.' | mail -s 'Test Email' your-email@example.com
-
-
-Check the recipient's inbox to confirm the delivery of the test email. If the email is delivered successfully, it indicates that your email server is configured correctly and functioning. Please check your MTA logs in case of issues.
-
-

Configuring Gogios


-
-To configure Gogios, create a JSON configuration file (e.g., /etc/gogios.json). Here's an example configuration:
-
- -
{
-  "EmailTo": "paul@dev.buetow.org",
-  "EmailFrom": "gogios@buetow.org",
-  "CheckTimeoutS": 10,
-  "CheckConcurrency": 2,
-  "StateDir": "/var/run/gogios",
-  "Checks": {
-    "Check ICMP4 www.foo.zone": {
-      "Plugin": "/usr/local/libexec/nagios/check_ping",
-      "Args": [ "-H", "www.foo.zone", "-4", "-w", "50,10%", "-c", "100,15%" ],
-      "Retries": 3,
-      "RetryInterval": 10
-    },
-    "Check ICMP6 www.foo.zone": {
-      "Plugin": "/usr/local/libexec/nagios/check_ping",
-      "Args": [ "-H", "www.foo.zone", "-6", "-w", "50,10%", "-c", "100,15%" ],
-      "Retries": 3,
-      "RetryInterval": 10
-    },
-    "www.foo.zone HTTP IPv4": {
-      "Plugin": "/usr/local/libexec/nagios/check_http",
-      "Args": ["www.foo.zone", "-4"],
-      "DependsOn": ["Check ICMP4 www.foo.zone"]
-    },
-    "www.foo.zone HTTP IPv6": {
-      "Plugin": "/usr/local/libexec/nagios/check_http",
-      "Args": ["www.foo.zone", "-6"],
-      "DependsOn": ["Check ICMP6 www.foo.zone"]
-    }
-    "Check NRPE Disk Usage foo.zone": {
-      "Plugin": "/usr/local/libexec/nagios/check_nrpe",
-      "Args": ["-H", "foo.zone", "-c", "check_disk", "-p", "5666", "-4"]
-    }
-  }
-}
-
-
-
    -
  • EmailTo: Specifies the recipient of the email notifications.
  • -
  • EmailFrom: Indicates the sender's email address for email notifications.
  • -
  • CheckTimeoutS: Sets the timeout for checks in seconds.
  • -
  • CheckConcurrency: Determines the number of concurrent checks that can run simultaneously.
  • -
  • StateDir: Specifies the directory where Gogios stores its persistent state in a state.json file.
  • -
  • Checks: Defines a list of checks to be performed, each with a unique name, plugin path, and arguments.
  • -

-Adjust the configuration file according to your needs, specifying the checks you want Gogios to perform.
-
-If you want to execute checks only when another check succeeded (status OK), use DependsOn. In the example above, the HTTP checks won't run when the hosts aren't pingable. They will show up as UNKNOWN in the report.
-
-Retries and RetryInterval are optional check configuration parameters. In case of failure, Gogios will retry Retries times each RetryInterval seconds.
-
-For remote checks, use the check_nrpe plugin. You also need to have the NRPE server set up correctly on the target host (out of scope for this document).
-
-The state.json file mentioned above keeps track of the monitoring state and check results between Gogios runs, enabling Gogios only to send email notifications when there are changes in the check status.
-
-

Running Gogios


-
-Now it is time to give it a first run. On OpenBSD, do:
-
- -
doas -u _gogios /usr/local/bin/gogios -cfg /etc/gogios.json
-
-
-To run Gogios via CRON on OpenBSD as the gogios user and check all services once per minute, follow these steps:
-
-Type doas crontab -e -u _gogios and press Enter to open the crontab file for the _gogios user for editing and add the following lines to the crontab file:
-
-
-*/5 8-22 * * * /usr/local/bin/gogios -cfg /etc/gogios.json
-0 7 * * * /usr/local/bin/gogios -renotify -cfg /etc/gogios.json
-
-
-Gogios is now configured to run every five minutes from 8 am to 10 pm via CRON as the _gogios user. It will execute the checks and send monitoring status whenever a check status changes via email according to your configuration. Also, Gogios will run once at 7 am every morning and re-notify all unhandled alerts as a reminder.
-
-

High-availability


-
-To create a high-availability Gogios setup, you can install Gogios on two servers that will monitor each other using the NRPE (Nagios Remote Plugin Executor) plugin. By running Gogios in alternate CRON intervals on both servers, you can ensure that even if one server goes down, the other will continue monitoring your infrastructure and sending notifications.
-
-
    -
  • Install Gogios on both servers following the compilation and installation instructions provided earlier.
  • -
  • Install the NRPE server (out of scope for this document) and plugin on both servers. This plugin allows you to execute Nagios check scripts on remote hosts.
  • -
  • Configure Gogios on both servers to monitor each other using the NRPE plugin. Add a check to the Gogios configuration file (/etc/gogios.json) on both servers that uses the NRPE plugin to execute a check script on the other server. For example, if you have Server A and Server B, the configuration on Server A should include a check for Server B, and vice versa.
  • -
  • Set up alternate CRON intervals on both servers. Configure the CRON job on Server A to run Gogios at minutes 0, 10, 20, ..., and on Server B to run at minutes 5, 15, 25, ... This will ensure that if one server goes down, the other server will continue monitoring and sending notifications.
  • -
  • Gogios doesn't support clustering. So it means when both servers are up, unhandled alerts will be notified via E-Mail twice; from each server once. That's the trade-off for simplicity.
  • -

-There are plans to make it possible to execute certain checks only on certain nodes (e.g. on elected leader or master nodes). This is still in progress (check out my Gorum Git project).
-
-

Conclusion:


-
-Gogios is a lightweight and straightforward monitoring tool that is perfect for small-scale environments. With its compatibility with the Nagios Check API, email notifications, and CRON-based scheduling, Gogios offers an easy-to-use solution for those looking to monitor a limited number of resources. I personally use it to execute around 500 checks on my personal server infrastructure. I am very happy with this solution.
-
-E-Mail your comments to paul@nospam.buetow.org :-)
-
-Other KISS-related posts are:
-
-2024-04-01 KISS high-availability with OpenBSD
-2023-10-29 KISS static web photo albums with photoalbum.sh
-2023-06-01 KISS server monitoring with Gogios (You are currently reading this)
-2021-09-12 Keep it simple and stupid
-
Back to the main site
diff --git a/gemfeed/index.gmi b/gemfeed/index.gmi index f6e86b4d..302b9dd9 100644 --- a/gemfeed/index.gmi +++ b/gemfeed/index.gmi @@ -2,6 +2,7 @@ ## To be in the .zone! +=> ./2025-09-14-bash-golf-part-4.gmi 2025-09-14 - Bash Golf Part 4 => ./2025-08-15-random-weird-things-iii.gmi 2025-08-15 - Random Weird Things - Part Ⅲ => ./2025-08-05-local-coding-llm-with-ollama.gmi 2025-08-05 - Local LLM for Coding with Ollama on macOS => ./2025-07-14-f3s-kubernetes-with-freebsd-part-6.gmi 2025-07-14 - f3s: Kubernetes with FreeBSD - Part 6: Storage diff --git a/index.gmi b/index.gmi index bb13cbbb..38a27ce5 100644 --- a/index.gmi +++ b/index.gmi @@ -1,6 +1,6 @@ # Hello! -> This site was generated at 2025-09-11T11:13:29+03:00 by `Gemtexter` +> This site was generated at 2025-09-13T12:04:03+03:00 by `Gemtexter` Welcome to the foo.zone! @@ -38,6 +38,7 @@ Everything you read on this site is my personal opinion and experience. You can ### Posts +=> ./gemfeed/2025-09-14-bash-golf-part-4.gmi 2025-09-14 - Bash Golf Part 4 => ./gemfeed/2025-08-15-random-weird-things-iii.gmi 2025-08-15 - Random Weird Things - Part Ⅲ => ./gemfeed/2025-08-05-local-coding-llm-with-ollama.gmi 2025-08-05 - Local LLM for Coding with Ollama on macOS => ./gemfeed/2025-07-14-f3s-kubernetes-with-freebsd-part-6.gmi 2025-07-14 - f3s: Kubernetes with FreeBSD - Part 6: Storage diff --git a/uptime-stats.gmi b/uptime-stats.gmi index 024fc16e..259c51a1 100644 --- a/uptime-stats.gmi +++ b/uptime-stats.gmi @@ -1,6 +1,6 @@ # My machine uptime stats -> This site was last updated at 2025-09-11T11:13:29+03:00 +> This site was last updated at 2025-09-13T12:04:03+03:00 The following stats were collected via `uptimed` on all of my personal computers over many years and the output was generated by `guprecords`, the global uptime records stats analyser of mine. -- cgit v1.2.3
+

Bash Golf Part 4


+
+This is the fourth blog post about my Bash Golf series. This series is random Bash tips, tricks, and weirdnesses I have encountered over time.
+
+
2021-11-29 Bash Golf Part 1
+2022-01-01 Bash Golf Part 2
+2023-12-10 Bash Golf Part 3
+2025-09-14 Bash Golf Part 4 (You are currently reading this)
+
+
+    '\       '\        '\        '\                   .  .        |>18>>
+      \        \         \         \              .         ' .   |
+     O>>      O>>       O>>       O>>         .                 'o |
+      \       .\. ..    .\. ..    .\. ..   .                      |
+      /\    .  /\     .  /\     .  /\    . .                      |
+     / /   .  / /  .'.  / /  .'.  / /  .'    .                    |
+jgs^^^^^^^`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+                        Art by Joan Stark, mod. by Paul Buetow
+
+
+

Table of Contents


+
+
+

Split pipelines with tee + process substitution


+
+Sometimes you want to fan out one stream to multiple consumers and still continue the original pipeline. tee plus process substitution does exactly that:
+
+
+somecommand \
+    | tee >(command1) >(command2) \
+    | command3
+
+
+All of command1, command2, and command3 see the output of somecommand. Example:
+
+ +
printf 'a\nb\n' \
+    | tee >(sed 's/.*/X:&/; s/$/ :c1/') >(tr a-z A-Z | sed 's/$/ :c2/') \
+    | sed 's/$/ :c3/'
+
+
+Output:
+
+
+a :c3
+b :c3
+A :c2 :c3
+B :c2 :c3
+X:a :c1 :c3
+X:b :c1 :c3
+
+
+This relies on Bash process substitution (>(...)). Make sure your shell is Bash and not a POSIX /bin/sh.
+
+Example (fails under dash/POSIX sh):
+
+ +
/bin/sh -c 'echo hi | tee >(cat)'
+# /bin/sh: 1: Syntax error: "(" unexpected
+
+
+Combine with set -o pipefail if failures in side branches should fail the whole pipeline.
+
+Example:
+
+ +
set -o pipefail
+printf 'ok\n' | tee >(false) | cat >/dev/null
+echo $?   # 1 because a side branch failed
+
+
+Further reading:
+
+Splitting pipelines with tee
+
+

Heredocs for remote sessions (and their gotchas)


+
+Heredocs are great to send multiple commands over SSH in a readable way:
+
+ +
ssh "$SSH_USER@$SSH_HOST" <<EOF
+    # Go to the work directory
+    cd "$WORK_DIR"
+  
+    # Make a git pull
+    git pull
+  
+    # Export environment variables required for the service to run
+    export AUTH_TOKEN="$APP_AUTH_TOKEN"
+  
+    # Start the service
+    docker compose up -d --build
+EOF
+
+
+Tips:
+
+Quoting the delimiter changes interpolation. Use <<'EOF' to avoid local expansion and send the content literally.
+
+Example:
+
+ +
FOO=bar
+cat <<'EOF'
+$FOO is not expanded here
+EOF
+
+
+Prefer explicit quoting for variables (as above) to avoid surprises. Example (spaces preserved only when quoted):
+
+ +
WORK_DIR="/tmp/my work"
+ssh host <<EOF
+    cd $WORK_DIR      # may break if unquoted
+    cd "$WORK_DIR"   # safe
+EOF
+
+
+Consider set -euo pipefail at the top of the remote block for stricter error handling. Example:
+
+ +
ssh host <<'EOF'
+    set -euo pipefail
+    false   # causes immediate failure
+    echo never
+EOF
+
+
+Indent-friendly variant: use a dash to strip leading tabs in the body:
+
+ +
cat <<-EOF > script.sh
+	#!/usr/bin/env bash
+	echo "tab-indented content is dedented"
+EOF
+
+
+Further reading:
+
+Heredoc headaches and fixes
+
+

Namespacing and dynamic dispatch with ::


+
+You can emulate simple namespacing by encoding hierarchy in function names. One neat pattern is pseudo-inheritance via a tiny super helper that maps pkg::lang::action to a pkg::base::action default.
+
+ +
#!/usr/bin/env bash
+set -euo pipefail
+
+super() {
+    local -r fn=${FUNCNAME[1]}
+    # Split name on :: and dispatch to base implementation
+    local -a parts=( ${fn//::/ } )
+    "${parts[0]}::base::${parts[2]}" "$@"
+}
+
+foo::base::greet() { echo "base: $@"; }
+foo::german::greet()  { super "Guten Tag, $@!"; }
+foo::english::greet() { super "Good day,  $@!"; }
+
+for lang in german english; do
+    foo::$lang::greet Paul
+done
+
+
+Output:
+
+
+base: Guten Tag, Paul!
+base: Good day,  Paul!
+
+
+

Indirect references with namerefs


+
+declare -n creates a name reference — a variable that points to another variable. It’s cleaner than eval for indirection:
+
+ +
user_name=paul
+declare -n ref=user_name
+echo "$ref"       # paul
+ref=julia
+echo "$user_name" # julia
+
+
+Output:
+
+
+paul
+julia
+
+
+Namerefs are local to functions when declared with local -n. Requires Bash ≥4.3.
+
+You can also construct the target name dynamically:
+
+ +
make_var() {
+    local idx=$1; shift
+    local name="slot_$idx"
+    printf -v "$name" '%s' "$*"   # create variable slot_$idx
+}
+
+get_var() {
+    local idx=$1
+    local -n ref="slot_$idx"      # bind ref to slot_$idx
+    printf '%s\n' "$ref"
+}
+
+make_var 7 "seven"
+get_var 7
+
+
+Output:
+
+
+seven
+
+
+

Function declaration forms


+
+All of these work in Bash, but only the first one is POSIX-ish:
+
+ +
foo() { echo foo; }
+function foo { echo foo; }
+function foo() { echo foo; }
+
+
+Recommendation: prefer name() { ... } for portability and consistency.
+
+

Chaining function calls in conditionals


+
+Functions return a status like commands. You can short-circuit them in conditionals:
+
+ +
deploy_check() { test -f deploy.yaml; }
+smoke_test()   { curl -fsS http://localhost/healthz >/dev/null; }
+
+if deploy_check || smoke_test; then
+    echo "All good."
+else
+    echo "Something failed." >&2
+fi
+
+
+You can also compress it golf-style:
+
+ +
deploy_check || smoke_test && echo ok || echo fail >&2
+
+
+

Grep, sed, awk quickies


+
+Word match and context: grep -w word file; with context: grep -C3 foo file (same as -A3 -B3). Example:
+
+ +
cat > /tmp/ctx.txt <<EOF
+one
+foo
+two
+three
+bar
+EOF
+grep -C1 foo /tmp/ctx.txt
+
+
+Output:
+
+
+one
+foo
+two
+
+
+Skip a directory while recursing: grep -R --exclude-dir=foo 'bar' /path. Example:
+
+ +
mkdir -p /tmp/golf/foo /tmp/golf/src
+printf 'bar\n' > /tmp/golf/src/a.txt
+printf 'bar\n' > /tmp/golf/foo/skip.txt
+grep -R --exclude-dir=foo 'bar' /tmp/golf
+
+
+Output:
+
+
+/tmp/golf/src/a.txt:bar
+
+
+Insert lines with sed: sed -e '1isomething' -e '3isomething' file. Example:
+
+ +
printf 'A\nB\nC\n' > /tmp/s.txt
+sed -e '1iHEAD' -e '3iMID' /tmp/s.txt
+
+
+Output:
+
+
+HEAD
+A
+B
+MID
+C
+
+
+Drop last column with awk: awk 'NF{NF-=1};1' file. Example:
+
+ +
printf 'a b c\nx y z\n' > /tmp/t.txt
+cat /tmp/t.txt
+echo
+awk 'NF{NF-=1};1' /tmp/t.txt
+
+
+Output:
+
+
+a b c
+x y z
+
+a b
+x y
+
+
+

Safe xargs with NULs


+
+Avoid breaking on spaces/newlines by pairing find -print0 with xargs -0:
+
+ +
find . -type f -name '*.log' -print0 | xargs -0 rm -f
+
+
+Example with spaces and NULs only:
+
+ +
printf 'a\0b c\0' | xargs -0 -I{} printf '<%s>\n' {}
+
+
+Output:
+
+
+<a>
+<b c>
+
+
+

Efficient file-to-variable and arrays


+
+Read a whole file into a variable without spawning cat:
+
+ +
cfg=$(<config.ini)
+
+
+Read lines into an array safely with mapfile (aka readarray):
+
+ +
mapfile -t lines < <(grep -v '^#' config.ini)
+printf '%s\n' "${lines[@]}"
+
+
+Assign formatted strings without a subshell using printf -v:
+
+ +
printf -v msg 'Hello %s, id=%04d' "$USER" 42
+echo "$msg"
+
+
+Output:
+
+
+Hello paul, id=0042
+
+
+Read NUL-delimited data (pairs well with -print0):
+
+ +
mapfile -d '' -t files < <(find . -type f -print0)
+printf '%s\n' "${files[@]}"
+
+
+

Quick password generator


+
+Pure Bash with /dev/urandom:
+
+ +
LC_ALL=C tr -dc 'A-Za-z0-9_' </dev/urandom | head -c 16; echo
+
+
+Alternative using openssl:
+
+ +
openssl rand -base64 16 | tr -d '\n' | cut -c1-22
+
+
+

yes for automation


+
+yes streams a string repeatedly; handy for feeding interactive commands or quick load generation:
+
+ +
yes | rm -r large_directory        # auto-confirm
+yes n | dangerous-command          # auto-decline
+yes anything | head -n1            # prints one line: anything
+
+
+

Forcing true to fail (and vice versa)


+
+You can shadow builtins with functions:
+
+ +
true()  { return 1; }
+false() { return 0; }
+
+true  || echo 'true failed'
+false && echo 'false succeeded'
+
+# Bypass function with builtin/command
+builtin true # returns 0
+command true # returns 0
+
+
+To disable a builtin entirely: enable -n true (re-enable with enable true).
+
+Further reading:
+
+Force true to return false
+
+

Restricted Bash


+
+bash -r (or rbash) starts a restricted shell that limits potentially dangerous actions, for example:
+
+
    +
  • Changing directories (cd).
  • +
  • Modifying PATH, SHELL, BASH_ENV, or ENV.
  • +
  • Redirecting output.
  • +
  • Running commands with / in the name.
  • +
  • Using exec.
  • +

+It’s a coarse sandbox for highly constrained shells; read man bash (RESTRICTED SHELL) for details and caveats.
+
+Example session:
+
+ +
rbash -c 'cd /'            # cd: restricted
+rbash -c 'PATH=/tmp'       # PATH: restricted
+rbash -c 'echo hi > out'   # redirection: restricted
+rbash -c '/bin/echo hi'    # commands with /: restricted
+rbash -c 'exec ls'         # exec: restricted
+
+
+

Useless use of cat (and when it’s ok)


+
+Avoid the extra process if a command already reads files or STDIN:
+
+ +
# Prefer
+grep -i foo file
+<file grep -i foo        # or feed via redirection
+
+# Over
+cat file | grep -i foo
+
+
+But for interactive composition, or when you truly need to concatenate multiple sources into a single stream, cat is fine, as you may think, "First I need the content, then I do X." Changing the "useless use of cat" in retrospect is really a waste of time for one-time interactive use:
+
+ +
cat file1 file2 | grep -i foo
+
+
+From notes: “Good for interactivity; Useless use of cat” — use judgment.
+
+

Atomic locking with mkdir


+
+Portable advisory locks can be emulated with mkdir because it’s atomic:
+
+ +
lockdir=/tmp/myjob.lock
+if mkdir "$lockdir" 2>/dev/null; then
+    trap 'rmdir "$lockdir"' EXIT INT TERM
+    # critical section
+    do_work
+else
+    echo "Another instance is running" >&2
+    exit 1
+fi
+
+
+This works well on Linux. Remove the lock in trap so crashes don’t leave stale locks.
+
+

Smarter globs and faster find-exec


+
+
    +
  • Enable extended globs when useful: shopt -s extglob; then patterns like !(tmp|cache) work.
  • +
  • Use -exec ... {} + to batch many paths in fewer process invocations:
  • +

+ +
find . -name '*.log' -exec gzip -9 {} +
+
+
+Example for extglob (exclude two dirs from listing):
+
+ +
shopt -s extglob
+ls -d -- !(.git|node_modules) 2>/dev/null
+
+
+E-Mail your comments to paul@nospam.buetow.org :-)
+
+Other related posts are:
+
+2025-09-14 Bash Golf Part 4 (You are currently reading this)
+2023-12-10 Bash Golf Part 3
+2022-01-01 Bash Golf Part 2
+2021-11-29 Bash Golf Part 1
+2021-06-05 Gemtexter - One Bash script to rule it all
+2021-05-16 Personal Bash coding style guide
+
+Back to the main site
+