Using Command Groups to Sort Multiple Commands in a Shell Script

You may want to combine the output of multiple commands and then sort all of them together. You can use command groups.
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.