Bash

OS / Distro Detection

admin by @admin ADMIN
5d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Read /etc/os-release for distro info — it's the standard on every modern Linux. Branch deploy scripts on distro to pick apt vs. dnf vs. apk.
Bash
Raw
# Source the file — it sets shell variables
. /etc/os-release
echo "Distro: $NAME"
echo "Version: $VERSION_ID"
echo "ID: $ID"
echo "Codename: $VERSION_CODENAME"

# Branch on distro
case "$ID" in
    ubuntu|debian)
        apt update && apt install -y "$@"
        ;;
    rhel|centos|fedora|rocky|almalinux)
        dnf install -y "$@" || yum install -y "$@"
        ;;
    alpine)
        apk add --no-cache "$@"
        ;;
    *)
        echo "Unsupported distro: $ID" >&2; exit 1 ;;
esac

# Detect macOS
[[ "$(uname)" == "Darwin" ]] && echo "macOS"

# Detect WSL
grep -qi microsoft /proc/version 2>/dev/null && echo "WSL"
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.