DEV Community

Wesley Schwengle
Wesley Schwengle

Posted on

Bash-ing on nodejs: build your first CLI tool with Bash (or Zsh)

I came across a post tagged both #cli and #webdev, titled "Build Your First CLI Tool". I was curious, CLI and web dev? Are we building a small Postman-like CLI tool to talk to API's?

The post walked through creating a CLI tool using JavaScript, Commander.js, and npm. You'd end up with a calc executable that could be globally installed with npm link.

Two things stood out:

  1. How is this getting engagement?
  2. Why are we using npm to build a CLI tool for something we already have?

You want a calculator? Try this:

echo "4 + 10" | bc -l
Enter fullscreen mode Exit fullscreen mode

Or if you want a REPL-like interface:

bc -l
Enter fullscreen mode Exit fullscreen mode

That’s built-in. No dependencies. No manifests. No node_modules.


What’s Missing

(and why it matters)

The post mentioned ls, echo, and cd. But that was it. It skipped every other tool known to someone who spends time on the CLI: awk, sed, bc, cat, cut, seq, wc, less, or date. The stuff you actually use in scripts. It felt like they were teaching someone to cook by unboxing a microwave.

A real first CLI tool

If you want to start writing CLI tools, start here:

#!/usr/bin/env bash
echo "Hello, CLI."
Enter fullscreen mode Exit fullscreen mode

Save it as hello.sh, mark it executable (chmod +x ./hello.sh), and put it in your $PATH. If you are a beginner and unfamiliar with $PATH, it's a special environment variable the shell uses to locate executable files. Various operating systems have different defaults. Adding a location to a path is easy:

PATH=$PATH:/new/path
Enter fullscreen mode Exit fullscreen mode

It's good to check if $HOME/bin and $HOME/.local/bin are used in $PATH, if not, add them. Bash users may use .bashrc and ZSH users will most likely use .zshrc.

If you place hello.sh in $HOME/bin, you can now execute the script. ZSH users can use rehash to force finding the script, and bash users can use hash -r. Be aware that first find wins, so if you have hello.sh in $HOME/bin and in $HOME/.local/bin, and $HOME/.local/bin is first located in your $PATH, that one is used.

The shell is all you need for your first CLI tool

Bash and Zsh are shells, ways to "talk" to your computer and tell it what to do. You can do this programmatically, with a script or directly in your prompt. So it's not just starting applications, such as your NodeJS calc executable. You can do for and while-loops, you have loop control (break and continue), switch statements, it's called case, but you get the idea. The shell is a powerful thing to master.

for i in *.csv
do
echo "$i $(basename $i .csv)"
done
Enter fullscreen mode Exit fullscreen mode

We now loop over all files with the .csv extension and print out the full filename and the filename without the extension. No npm, no JavaScript, just:

  • bash (most Linux distros ship with bash as a default)
  • zsh (MacOS default shell)
  • fish (a popular shell)
  • dash (on Debian-based distros for system scripts)

Resources

Want resources?

Bash Reference Manual – the official source

Bash Beginners Guide

Zsh manual - the official zsh documentation

Zsh User Guide – practical and readable

Classic Shell Scripting (book) – This was my deep dive into shell scripting.

I'm not knocking learning JavaScript. But if you're writing a CLI tool, learn the CLI.

Don't scaffold an entire JS project just to add two numbers.
Learn what the terminal already gives you. The best tools are often the ones you already have.

Top comments (0)