A simple bash script to install k9s - the Kubernetes terminal UI
- ✅ Automatically fetches the latest release version from GitHub
- ✅ Installs to
~/.local/binby default - ✅ Supports custom version and installation directory
- ✅ Displays installation status and version info
./k9s.sh./k9s.sh 0.27.4./k9s.sh 0.27.4 /usr/local/bin#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/_lib.sh"
GITHUB="derailed/k9s"
VERSION=${1:-"$(get_latest_release $GITHUB)"}
INSTALL_DIR=${2:-"$HOME/.local/bin"}
CMD=k9s
NAME="k9s terminal UI for Kubernetes"
pre_run
curl -sSL "https://github.com/$GITHUB/releases/download/v${VERSION}/k9s_Linux_amd64.tar.gz" | \
tar -zx -C "$INSTALL_DIR" $CMD
post_run versionThis script uses a shared _lib.sh library that provides:
get_latest_release <repo>- Fetches the latest GitHub release versionpre_run- Prepares installation (creates directory, checks PATH)post_run <flag>- Displays version info after installation
get_latest_release() {
if [ $# -lt 2 ]; then PREFIX="v"; else PREFIX=$2; fi
curl --silent "https://api.github.com/repos/$1/releases/latest" |
grep '"tag_name":' | sed -E "s/.*\"$PREFIX([^\"]+)\".*/\1/"
}
pre_run() {
echo -e "\e[34m»»» 📦 \e[32mInstalling \e[33m$NAME \e[35mv$VERSION\e[0m ..."
if [ -z "$INSTALL_DIR" ]; then return; fi
echo -e "\e[34m»»» 📂 \e[32mTarget directory for binary: \e[35m$INSTALL_DIR"
if [[ :$PATH: == *:"$INSTALL_DIR":* ]] ; then
echo -e "\e[34m»»» ✅ \e[32mPATH is good"
else
echo -e "\e[34m»»» 💥 \e[31mInstall directory in not in PATH. Temporarily adding it!"
echo -e "\e[34m»»» 📢 \e[31mNOTE! Amend your shell startup scripts to make this change permanent:\n\t\e[37mexport PATH=\$PATH:$INSTALL_DIR"
export PATH="$PATH:$INSTALL_DIR"
fi
mkdir -p "$INSTALL_DIR"
}
post_run() {
VERFLAG=${1:-"--version"}
echo -e "\n\e[34m»»» 💾 \e[32mInstalled to: \e[33m$(which $CMD)"
echo -e "\e[34m»»» 💡 \e[32mVersion details: \e[39m$($CMD $VERFLAG)"
}- Get Latest Version - Queries GitHub API for the latest k9s release
- Prepare - Creates
~/.local/binif needed, checks PATH configuration - Download & Extract - Downloads the Linux tarball and extracts the binary
- Verify - Displays the installation path and version
bash4.0+curltargrep&sed
MIT