Terminal Tools and Writing Workflows
Build a Bash CLI for Blogging from the Terminal
`writer-cli` is a Bash tool that creates, edits, builds and pushes blog posts from the terminal. The guide covers its modular design, config layering, frontmatter generation and setup wizard.
- By
- Brennan Kenneth Brown
- Published
- Reading time
- 8 minutes
- Level
- Intermediate
- You need
- Comfort with the command line
- Basic Bash
- A static blog in git
writer-cli is a Bash tool that creates, edits, builds and pushes a static-site blog post from the terminal with one command, writer my-post-slug. This guide walks through how it works (modular Bash, config layering, frontmatter generation, a setup wizard), the design decisions behind it, and how to install it. It works with Hugo, Eleventy or Jekyll sites kept in git.
I built it after thinking through the manual steps for a post on my own server: SSH in, navigate to the right folder, create a file with the right frontmatter (YAML or TOML) and date format, open an editor, write, save, run the build, check for errors, then git add, git commit and git push. It is inspired by TTBP, the tilde.town feels engine, which is a blogging tool for writing an entry, publishing it, and browsing other users' entries in the tildeverse. The Google Shell Style Guide, which Cris pointed me to, influenced much of the code. The codebase is now around 1,800 lines.
What does writer-cli do?
I wanted to wrap the full lifecycle of a Hugo (or Eleventy, or Jekyll) blog post into one shell command. When you run writer my-slug, the following happens:
- You're asked for a title and optional tags
- Your editor opens with the cursor below the frontmatter, ready to write
- You close the editor, optionally add a one-line description
- You confirm, and writer builds the site and pushes to git
The tool takes care of everything else.
How do I install writer-cli?
The command below clones the repo, symlinks writer, and updates your PATH if needed.
curl -fsSL https://raw.githubusercontent.com/brennanbrown/writer-cli/main/INSTALL.sh | bash
After the installer finishes, reload your shell profile so the new ~/.local/bin/ PATH entry takes effect and writer is available as a command:
source ~/.bashrc # or ~/.zshrc if you're using Zsh, etc.
Then run writer --setup to configure your site directory, SSG, and editor.
Note: Don't run
curl | bashcommands you haven't read. The installer and the whole repo are plain shell scripts with no build step, so you can read everything before running it.
The installer clones the repo to ~/.local/share/writer-cli, symlinks writer to ~/.local/bin/, and adds that to your PATH if needed. Everything in your home directory, nothing system-wide.
To uninstall: rm -rf ~/.local/share/writer-cli ~/.local/bin/writer.
The writer-cli docs site has the full guide, including the SSH remote workflow and .writerrc project-local config.
To report a bug or contribute, open an issue or submit a pull request. All tests should pass before submitting, and shellcheck writer.sh lib/*.sh tests/lib/*.sh lints your changes.
Why write the tool in Bash?
Bash is available on nearly every Linux server. The intended use case is a remote server over SSH. You can post from anywhere you can open a terminal: your laptop, a library computer, even your phone if you're tech-savvy enough.
My philosophy is that you should not be required to have dependencies installed just to publish a blog post, such as Python, Node, etc. Bash needs none of those.
I wanted to make a blogging tool that, itself was just a text file. There's no binaries, build steps, or compilation. You can read every line from source.
Of course, there is trade-off. bash 4 had a GPL license change, and so macOS ships with the outdated bash 3.2. This means no associative arrays, no ${var,,} lowercase operator, no mapfile.
I needed workarounds:
# What I wanted to write:
slug="${input,,}"
# What I had to write instead (bash 3.2 compatible):
slug="$(printf '%s' "$input" | tr '[:upper:]' '[:lower:]')"
It's more verbose and clunky, but it works on every machine.
How is writer-cli structured?
As I accounted for edge cases and different static-site generators, the single writer.sh script became 700 lines.
It worked, but I decided to modularize it, the way I did with a large Eleventy config:
lib/
├── defaults.sh ← global variables and output helpers
├── config.sh ← INI config file parser
├── args.sh ← CLI flag parsing
├── setup.sh ← interactive setup wizard
├── validate.sh ← slug validation, ISO 8601 dates
├── frontmatter.sh ← YAML and TOML builders
├── deps.sh ← dependency pre-flight checks
└── post.sh ← main workflow orchestration
The entry point writer.sh is now only 30 lines, it resolves its own directory (so symlinks work), sources all eight modules in order, and calls main "$@".
_writer_source="${BASH_SOURCE[0]}"
while [[ -L "$_writer_source" ]]; do
_writer_dir="$(cd "$(dirname "$_writer_source")" && pwd)"
_writer_source="$(readlink "$_writer_source")"
[[ "$_writer_source" != /* ]] && _writer_source="${_writer_dir}/${_writer_source}"
done
WRITER_DIR="$(cd "$(dirname "$_writer_source")" && pwd)"
source "${WRITER_DIR}/lib/defaults.sh"
source "${WRITER_DIR}/lib/config.sh"
# ... and so on
main "$@"
Using ${BASH_SOURCE[0]} rather than $0 is important, $0 can be rewritten by the shell, while ${BASH_SOURCE[0]} always points to the file being sourced or executed. The while loop then follows any chain of symlinks to the real file, handling the case where BSD readlink returns relative paths (unlike GNU readlink -f).
All modules are sourced into the same shell, not run as subshells. This means they share global state without needing export everywhere. It also means each lib file has no shebang and doesn't set its own set -e and instead inherits the caller's environment.
How does config layering work?
Writer has ten configurable settings: your SSG, build command, content directory, editor, timezone, etc. and I wanted them to be overridable at multiple levels without confusion.
The loading order is:
- Hardcoded defaults (in
lib/defaults.sh) - Global config (
~/.config/writer/config) cdtoSITE_DIRif set- Project-local
.writerrc - CLI flags — highest priority
load_global_config # reads ~/.config/writer/config
if [[ -n "$SITE_DIR" ]]; then
cd "$SITE_DIR"
fi
load_local_config # reads .writerrc in current directory
# then CLI flag overrides are applied
The config format is plain INI. KEY=value, one per line, # for comments. I wrote a small parser for it to continue not using an external library:
_parse_config_file() {
local config_file="$1"
local line key value
while IFS= read -r line || [[ -n "$line" ]]; do
line="${line%%#*}" # strip inline comments
line="${line#"${line%%[![:space:]]*}"}" # trim leading whitespace
line="${line%"${line##*[![:space:]]}"}" # trim trailing whitespace
if [[ -z "$line" ]]; then continue; fi
key="${line%%=*}"
value="${line#*=}"
# ... trim whitespace from each, then assign to global
case "$key" in
SSG) SSG="$value" ;;
BUILD_CMD) BUILD_CMD="$value" ;;
# ... etc
*)
err "Unknown config key: '$key'"
exit 5
;;
esac
done < "$config_file"
}
The || [[ -n "$line" ]] on the while read is important, the last line of a file is silently dropped if it doesn't end with a newline, a common mistake in hand-edited config files.
Unknown keys exit with code 5 and print all valid keys. It's strict to ensure typos aren't silently ignored.
How does writer-cli generate frontmatter?
Both YAML and TOML are supported, and the frontmatter needs to be generated twice: once before the editor opens (without a description, because you haven't written the post yet) and once after (optionally inserting a description you provide post-edit).
For YAML:
build_yaml_frontmatter() {
local title="$1" slug="$2" date="$3"
local tags="$4" description="$5" draft="$6"
local fm="---\n"
fm+="title: \"${title}\"\n"
fm+="date: ${date}\n"
fm+="slug: \"${slug}\"\n"
if [[ -n "$tags" ]]; then
fm+="tags:\n"
IFS=',' read -ra tag_array <<< "$tags"
for tag in "${tag_array[@]}"; do
# trim whitespace from each tag
tag="${tag#"${tag%%[![:space:]]*}"}"
tag="${tag%"${tag##*[![:space:]]}"}"
if [[ -z "$tag" ]]; then continue; fi
fm+=" - ${tag}\n"
done
fi
if [[ -n "$description" ]]; then
fm+="description: \"${description}\"\n"
fi
fm+="draft: ${draft}\n---\n"
printf "%b" "$fm"
}
I needed to trim the tags, because if a user types life, coffee , writing the commas split into life, coffee, writing. The whitespace needs to be stripped from each element. Bash doesn't have a trim function, so I use parameter expansion: ${var#"${var%%[![:space:]]*}"} strips leading whitespace by removing the longest prefix that consists only of spaces, and ${var%"${var##*[![:space:]]}"}" does the same for trailing. It's a little ugly, but it works without spawning a subshell.
After the editor closes, if the user provides a description, the tool only needs to rewrite the frontmatter section of the already-written file, preserving the post body. The closing delimiter is found (--- for YAML, +++ for TOML) and tail is used to extract the body:
local delim_count=0
local body_start=0
local lineno=0
while IFS= read -r line; do
lineno=$(( lineno + 1 ))
if [[ "$line" == "$delimiter" ]]; then
delim_count=$(( delim_count + 1 ))
if [[ $delim_count -eq 2 ]]; then
body_start=$lineno
break
fi
fi
done < "$file_path"
body="$(tail -n +"$((body_start + 1))" "$file_path")"
printf "%s\n" "$frontmatter" > "$file_path"
if [[ -n "$body" ]]; then
printf "%s\n" "$body" >> "$file_path"
fi
How do I get an ISO 8601 date in Bash on Linux and macOS?
Getting a timezone-aware ISO 8601 date string in bash is annoying. GNU date (Linux) and BSD date (macOS) have completely different flag syntax:
# GNU date (Linux):
date --iso-8601=seconds
# Output: 2026-05-23T12:00:00-06:00
# BSD date (macOS):
date +"%Y-%m-%dT%H:%M:%S%z"
# Output: 2026-05-23T12:00:00-0600 ← missing colon in offset
Hugo, for example, requires the colon. So get_iso_date() tries GNU date first, and if that fails, falls back to BSD date with a sed pass to insert the colon:
printf '%s' "$result" | sed 's/\([+-][0-9]\{2\}\)\([0-9]\{2\}\)$/\1:\2/'
For timezones, I wanted to avoid the error-prone approach of manually computing offsets, so the tool uses a plain string variable and a conditional TZ= prefix:
local custom_tz=""
if [[ "$TIMEZONE" != "auto" && -n "$TIMEZONE" ]]; then
custom_tz="$TIMEZONE"
fi
if [[ -n "$custom_tz" ]]; then
TZ="$custom_tz" date --iso-8601=seconds 2>/dev/null
else
date --iso-8601=seconds 2>/dev/null
fi
An earlier version used a bash array (local -a tz_prefix=()) to build an optional env TZ=... prefix, which looked cleaner. But under set -u in bash 3.2, expanding an empty array with ${tz_prefix[@]} throws unbound variable, a well-known bash 3.2 gotcha that surfaces when the array is empty. Once again, the conditional string approach is more clunky but works correctly everywhere.
How does the setup wizard work?
I wanted first-run setup to be approachable for less technical users. Running writer with no config fires an interactive wizard. There are prompts, and each shows the current default in green so you can just press Enter through everything.
_prompt() {
local key="$1" description="$2" current="$3"
printf "\n${CYAN}%s${RESET}\n" "$key"
printf " %s\n" "$description"
printf " Current: ${GREEN}%s${RESET}\n" "$current"
printf " Press Enter to keep, or type a new value: "
read -r REPLY_VAL </dev/tty || true
REPLY_VAL="${REPLY_VAL#"${REPLY_VAL%%[![:space:]]*}"}"
REPLY_VAL="${REPLY_VAL%"${REPLY_VAL##*[![:space:]]}"}"
if [[ -z "$REPLY_VAL" ]]; then REPLY_VAL="$current"; fi
}
The </dev/tty redirect is needed when the installer is run via curl | bash, because stdin is the pipe carrying the script itself. Without redirecting reads to /dev/tty, read would consume lines of the script as user input, silently corrupting the saved config. The whitespace trimming strips spaces a user might accidentally type before pressing Enter.
The result is saved to ~/.config/writer/config with a date-stamped comment header. It can be re-run anytime with writer --setup and every value is shown with whatever you set last time.
How is writer-cli tested?
The test suite covers edge cases and gives verbose output on failure. Run it with:
bash tests/test_writer.sh
The harness is split into a thin runner (tests/test_writer.sh) that sources six focused modules from tests/lib/, mirroring the same structure as the main lib/. The runner creates an isolated $HOME via mktemp -d for every test so the real config is never touched. All tests use --dry-run or temp directories, there's no real editor, build, or git operations ever run. It exits non-zero if anything fails, so it can be used in CI.
What would I do differently?
The eval for BUILD_CMD: Originally I used eval "$BUILD_CMD" to run the user's build command. This is discouraged by the Google Shell Style Guide because eval on user-controlled strings is a correctness and security risk. I've replaced it with bash -c "$BUILD_CMD", which still handles multi-word commands like hugo --minify correctly but is safer.
Test coverage of the full flow: The test suite has many assertions covering every function, all flag combinations, frontmatter formats, and config parsing. The actual editor-open-save-close flow is not automatically tested because it needs an interactive terminal. I did a lot of manual testing myself on four different machines and I hope that was robust enough to catch issues.
Bonus: how do I make a Homebrew tap?
I wanted more install options than the curl installer, since the curl | bash pattern is a security anti-pattern.
I added support for Basher, a lightweight package manager for shell scripts. Where Homebrew is a full package manager that handles binaries and dependencies, Basher is much simpler: it clones a GitHub repo and links the main script into your PATH. All it needs is a package.sh manifest in the repo declaring the entry point:
BASHER_PACKAGE_NAME=writer
BASHER_MAIN=writer.sh
Then anyone with Basher can install writer with:
basher install brennanbrown/writer-cli
I also then decided to make a Homebrew tap to support brew install, as that's the first thing a lot of macOS users reach for, though it's a little more complicated.
Homebrew supports third-party "taps", which are just GitHub repos named homebrew-<something> that contain a Formula/ directory.
So I created brennanbrown/homebrew-writer. The repo is one file:
homebrew-writer/
└── Formula/
└── writer.rb
The formula itself is Ruby:
class Writer < Formula
desc "CLI post creation tool for static site generator blogs"
homepage "https://github.com/brennanbrown/writer-cli"
url "https://github.com/brennanbrown/writer-cli/archive/refs/tags/v1.1.2.tar.gz"
sha256 "845e16dbf3c438196754d9a6956db265e5f371b5eb061531ca9b54670f2a5096"
license "AGPL-3.0-only"
depends_on "bash"
depends_on "git"
def install
libexec.install "writer.sh", "lib"
bin.install_symlink libexec/"writer.sh" => "writer"
end
def caveats
<<~EOS
Run the setup wizard before first use:
writer --setup
EOS
end
test do
assert_match "Usage:", shell_output("#{bin}/writer --help")
end
end
The symlink-resolution loop in writer.sh needs to handle relative paths. When Homebrew creates bin/writer → ../libexec/writer.sh, BSD readlink returns that relative path as-is, not an absolute one. The loop resolves it against the symlink's own directory:
[[ "$_writer_source" != /* ]] && _writer_source="${_writer_dir}/${_writer_source}"
Without that line, cd "$(dirname relative/path)" would resolve relative to whatever directory the user happened to be in when they ran writer.
Once the tap is in place, installing is:
brew tap brennanbrown/writer
brew install writer
And updating is just brew upgrade writer, so you don't need to manually pull from the git repository.