Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Using Command Groups to Sort Multiple Commands in a Shell Script

using-command-groups-to-sort-multiple-commands-in-a-shell-script.jpg

You may want to combine the output of multiple commands and then sort all of them together. You can use command groups.

Quick Jump:

I’ve written about command groups { ... } before when redirecting multiple commands to a file. It’s POSIX complaint and I like learning by example so here’s another use case.

Here’s a practical example from my dotfiles’ install script to quickly browse all of your local files by assembling a list of files, sorting them and then displaying them in fzf:

    {
      fd --type file --unrestricted --extension "local" .
      fd --type file --unrestricted ".local/bin/local"
      echo ".config/zsh/.zsh_history"
      echo "install-config"
    } | sort --unique | fzf --multi --preview='bat --color=always {}' --bind "ctrl-a:toggle-all"

The real takeaway is each of those commands within { ... } produce 1 or more lines of output, in this case relative path files.

In the context of my dotfiles, local files are your personalized non-git committed files. It’s handy to be able to see them all in 1 place.

The demo video breaks apart this command and shows how it works.

# Demo Video

Timestamps

  • 0:14 – Quick example using sort
  • 0:37 – Practical example to build a file browser with fzf

When was the last time you used command groups? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments