Skip to main content

Heredocs Can Make Your Bash Scripts Self-Documenting

· 3 min read

I have long since come to appreciate the value of writing scripts to avoid someone else (or future me) from having to re-learn and re-solve problems, but something about it has always bugged me.

I am automating a process, but I'm also documenting it, and those two things struggle to coexist.

One option is to write a bash script for the automation and a markdown file for the documentation, but they inevitably end up duplicating information and/or getting out of sync. The other is to just have a single markdown file with a bunch of inline bash that you manually copy into a terminal. But "running" it is clunky, tedious, and easy to mess up.

A CRDT-based Messenger in 12 Lines of Bash Using a Synced Folder

· 8 min read
mkdir -p $(dirname $0)/data; cd data

print_messages() {
clear
cat $(ls -tr | tail -n30)
printf "\033[31m$USER:\033[0m "
}
export -f print_messages

watchexec 2> /dev/null -- bash -c "print_messages" &

while read text; do
printf "\033[31m$USER:\033[0m $text\n\n" > "$(uuidgen)"
done