Gum – A Tool for Glamorous Shell Scripts in Linux

If you are a Linux operating system enthusiast or on a path to defining yourself as one, then crossing paths with shell scripting is completely inevitable. By definition, a shell script is a program designed to be executed on a Unix/Linux shell environment.

We can associate command-line interpreter (shell) with script operations like printing text, program execution, and file manipulation. Examples of popular Unix/Linux shell environments include GNU Bourne-Again Shell, Bourne Shell, Korn Shell, and C Shell.

Shell scripts that run in a Linux operating system environment are attributed with a .sh file extension. Consider the execution of the following shell script file:

$ ./onescript.sh
Run Shell Script in Linux
Run Shell Script in Linux

The execution of the above shell script is quite basic. If you mind the outlook of your shell scripts, then this article is for you. It will walk us through the installation as a usage of Gum to create glamorous shell scripts.

Gum is pre-packaged with ready-to-use and highly configurable utilities that enable Linux users to implement a few lines of code to successfully come up with applicable shell scripts and dotfiles aliases.

Gum Tool in Action
Gum Tool in Action

Installation of Gum in Linux

Before we can see what the Gum tool is capable of achieving, we need to install it on our Linux operating system distributions. Since it is not accessible in all major Linux package managers.

We are going to make use of the Homebrew package manager (installable on all major Linux distributions). If you do not already have Homebrew installed on your Linux distribution, refer to the Homebrew installation article that we wrote on it.

Once installed, you can use the Homebrew to install the gum tool.

$ brew install gum
Install Gum in Linux
Install Gum in Linux

Alternatively, you can install Gum on Debian/Ubuntu distributions using the following commands.

$ echo 'deb [trusted=yes] https://repo.charm.sh/apt/ /' | sudo tee /etc/apt/sources.list.d/charm.list
$ sudo apt update && sudo apt install gum

On Arch and Manjaro Linux, use:

$ sudo pacman -S gum

Gum Basic Usage in Linux

Several useful commands are associated with Gum:

Choose from List of Choices

The following command makes it possible to select an option from several listed choices.

$ echo "Make a card pick on any card..." CARD=$(gum choose --height 15 {{A,K,Q,J},{10..2}}" "{♠,♥,♣,♦})
List of Choices
List of Choices

The above command execution should be able to point out the user choice.

Input

With the following command, a user is prompted to provide a standard input on the terminal:

$ gum input > respond.txt
Shell Standard Input
Shell Standard Input

If you need the input to be sensitive, make use of the --password flag.

$ gum input --password > respond.txt

Write

The following command provides multi-line input:

$ gum write > my_story.txt
Linux Multi-Line Input
Linux Multi-Line Input

Filter

Makes it possible to filter a list of values via fuzzy matching.

$ echo red >> colors.txt
$ echo green >> colors.txt
$ echo blue >> colors.txt
$ cat colors.txt | gum filter > selector.txt

The execution of the above commands will lead to the following screen capture display:

Filter List of Values
Filter List of Values

We can filter through a list by typing a keyword.

Confirm

Makes it possible to confirm a user’s action before its execution.

$ gum confirm && rm new.txt || echo "Unable to locate file"
Confirm User Action
Confirm User Action

Spin

We can also display a spinner while a command/script executes while at the same time specifying the duration of the spin via the sleep command argument.

$ gum spin --spinner dot --title "Accessing LinuxShellTips Website..." -- sleep 10
Display Spinner
Display Spinner

If you were to create a shell script, your script file should have the following header.

#!/bin/sh
Create Shell Script in Linux
Create Shell Script in Linux

Make the script executable and run it.

$ chmod +x demo.sh
$ ./demo.sh 
Execute Script in Linux
Execute Script in Linux

More Gum usage references can be found in its user manual:

$ gum -h 

To reference the usage of a command-like format, run:

$ gum format -h 

This article guide makes it comfortably easy to begin and evolve in the creation of glamorous shell scripts.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.