cd /news/developer-tools/bash-functions ยท home โ€บ topics โ€บ developer-tools โ€บ article
[ARTICLE ยท art-8542] src=gist.github.com โ†— pub= topic=developer-tools verified=true sentiment=ยท neutral

bash_functions

Bash function called `sys` that displays a system dashboard in the terminal. It collects and color-codes CPU usage, memory usage, disk usage, load average, uptime, and next backup time, then prints them with icons and a storage breakdown. The function uses thresholds to color metrics green, yellow, or red based on usage levels.

read5 min views16 publishedMay 21, 2026

Created May 21, 2026 08:47

Save xhaythemx/573a1000e3ecb1a7a7e8cca622f50e68 to your computer and use it in GitHub Desktop.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters| # ~/.bash_functions | | | sys() { | | | # โ”€โ”€ Colors โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ | |

| local R=$'\e[0m' | |
| local B=$'\e[1m' | |
| local DIM=$'\e[2m' | |
| local CYA=$'\e[38;5;110m' | |
| local WHT=$'\e[38;5;255m' | |
| local LBL=$'\e[38;5;250m' | |
| local YEL=$'\e[38;5;179m' | |
| local GRN=$'\e[38;5;108m' | |
| local RED=$'\e[38;5;167m' | |
| local MAG=$'\e[38;5;183m' | |
| local SEP=$'\e[38;5;59m' | |

| # โ”€โ”€ Collect stats โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ | | | read load1 _ _ _ < /proc/loadavg | | | local cpu_pct | | | cpu_pct=$(awk '/^cpu /{u=$2+$4; t=$2+$3+$4+$5; printf "%.0f", u*100/t}' /proc/stat) | | | local mem_pct mem_used mem_total | |

| read mem_total mem_used < <(free -m | awk '/Mem:/{print $2, $3}') | |
| mem_pct=$(awk "BEGIN{printf \"%.0f\", ($mem_used/$mem_total)*100}") | |

| local disk_pct | | | disk_pct=$(df / | awk 'NR==2{gsub(/%/,"",$5); print $5}') | | | local up_str | |

| up_str=$(awk '{ | |
| s = int($1) | |
| d = int(s/86400); s -= d*86400 | |
| h = int(s/3600); s -= h*3600 | |
| m = int(s/60) | |

| if (d) printf "%dd %dh %dm", d, h, m | | | else if (h) printf "%dh %dm", h, m | | | else printf "%dm", m | | | }' /proc/uptime) | | | local next_backup | | | next_backup=$(systemctl show borgmatic.timer --property=NextElapseUSecRealtime | cut -d= -f2 | awk '{print $1, $2}') | | | # โ”€โ”€ Threshold colors โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ | | | local cpu_col ram_col dsk_col | |

| if [ "$cpu_pct" -ge 80 ]; then cpu_col="$RED" | |
| elif [ "$cpu_pct" -ge 50 ]; then cpu_col="$YEL" | |

| else cpu_col="$GRN" | | | fi | |

| if [ "$mem_pct" -ge 80 ]; then ram_col="$RED" | |
| elif [ "$mem_pct" -ge 60 ]; then ram_col="$YEL" | |

| else ram_col="$GRN" | | | fi | |

| if [ "$disk_pct" -ge 80 ]; then dsk_col="$RED" | |
| elif [ "$disk_pct" -ge 50 ]; then dsk_col="$YEL" | |

| else dsk_col="$GRN" | | | fi | | | # โ”€โ”€ Header โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ | | | printf "\n" | |

| printf "${SEP}โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”${R}\n" | |
| printf "${CYA}${B} System Dashboard${R}\n" | |
| printf "${SEP}โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”${R}\n" | |

| # โ”€โ”€ Status bar โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ | | | printf "\n" | |

| printf " ${cpu_col} ${B}${cpu_pct}%%${R} " | |
| printf "${SEP}โ”‚${R} " | |
| printf "${ram_col} ${B}${mem_pct}%%${R} " | |
| printf "${SEP}โ”‚${R} " | |
| printf "${dsk_col} ${B}${disk_pct}%%${R} " | |
| printf "${SEP}โ”‚${R} " | |
| printf "${LBL} ${WHT}${load1}${R} " | |
| printf "${SEP}โ”‚${R} " | |
| printf "${LBL} ${WHT}${up_str}${R} " | |
| printf "${SEP}โ”‚${R} " | |
| printf "${CYA} ${WHT}${next_backup}${R}\n" | |

| # โ”€โ”€ Storage โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ | |

| printf "\n ${CYA}${B} Storage${R}\n" | |
| printf " ${SEP}โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€${R}\n" | |
| df -hT -x tmpfs -x devtmpfs | awk \ | |
| -v R="$R" -v DIM="$DIM" \ | |
| -v CYA="$CYA" -v WHT="$WHT" -v LBL="$LBL" \ | |
| -v YEL="$YEL" -v GRN="$GRN" -v RED="$RED" \ | |
| -v MAG="$MAG" -v SEP="$SEP" ' | |
| $2 ~ /^(ext4|btrfs|xfs)$/ { | |
| gsub(/%/, "", $6) | |

| pct = $6 + 0 | |

| if (pct < 50) col = GRN | |
| else if (pct < 80) col = YEL | |

| else col = RED | | | bar = "" | |

| n = int(pct / 5) | |
| for (i = 0; i < 20; i++) | |
| bar = bar (i < n ? "โ–ˆ" : "โ–‘") | |
| if ($7 == "/") { | |

| icon = "" | | | label = "OS SSD" | | | } else { | | | icon = "" | | | label = "DATA SSD" | | | } | | | printf " %s%s %-10s%s %s%-5s%s %ssize%s %-7s %sused%s %-7s %sfree%s %-7s %s[%s]%s %s%3s%%%s %s%s%s\n", | | | MAG, icon, label, R, | | | DIM, $2, R, | | | LBL, WHT, $3, | | | LBL, WHT, $4, | | | LBL, WHT, $5, | | | col, bar, R, | | | col, pct, R, | | | SEP, $7, R | | | }' | | | # โ”€โ”€ Temperatures โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ | |

| printf "\n ${CYA}${B} Temperatures${R}\n" | |
| printf " ${SEP}โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€${R}\n" | |
| local labels=("OS SSD" "DATA SSD") | |

| local i=0 | | | for disk in /dev/nvme0n1 /dev/nvme1n1; do | | | local raw temp_c | | | raw=$(sudo nvme smart-log "$disk" 2>/dev/null | grep -i '^temperature') | | | if echo "$raw" | grep -qi 'celsius|ยฐC'; then | | | temp_c=$(echo "$raw" | grep -oE '[0-9]+' | head -n1) | | | elif echo "$raw" | grep -qi 'fahrenheit|ยฐF'; then | | | local f | |

| f=$(echo "$raw" | grep -oE '[0-9]+' | head -n1) | |
| [ -n "$f" ] && temp_c=$(awk "BEGIN{printf \"%.0f\", ($f-32)*5/9}") | |

| else | | | temp_c=$(echo "$raw" | grep -oE '[0-9]+' | head -n1) | | | fi | | | local t_col | |

| if [ "${temp_c:-0}" -ge 65 ]; then t_col="$RED" | |
| elif [ "${temp_c:-0}" -ge 50 ]; then t_col="$YEL" | |

| else t_col="$GRN" | | | fi | |

| local fill=0 bar="" | |
| [ -n "$temp_c" ] && fill=$(( (temp_c < 100 ? temp_c : 99) * 20 / 100 )) | |
| for (( j=0; j<20; j++ )); do | |
| [ $j -lt $fill ] && bar+="โ–ˆ" || bar+="โ–‘" | |

| done | |

| printf " ${CYA} ${MAG}%-10s${R} ${t_col}${B}%sยฐC${R} ${DIM}[${t_col}%s${DIM}]${R}\n" \ | |
| "${labels[$i]}" "${temp_c:---}" "$bar" | |
| (( i++ )) | |

| done | | | # โ”€โ”€ Footer โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ | | | printf "\n${SEP}โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”${R}\n\n" | | | } |

โ”€โ”€ more in #developer-tools 4 stories ยท sorted by recency
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain โ€” perfect for shipping the agent you just read about.

$git push zahid main
โ†’ Live at https://your-agent.zahid.host โœ“
Get free account โ†’ Pricing
from โ‚ฌ0/mo ยท no card required
LIVE [news/bash-functions] indexed:0 read:5min 2026-05-21 ยท โ€”