DEV Community

Beta Shorts
Beta Shorts

Posted on

2

Building a System Information Fetch Tool in Bash

I once needed a quick system overview—CPU load, memory usage, disk space—but opening multiple commands felt inefficient. Instead of running top, df -h, and free -m separately, I decided to automate everything in a single Bash script.

The result? A custom system fetch tool that provides essential system details at a glance. In this guide, you'll learn how to build your own step by step, improving your Bash scripting skills along the way.


Step 1: The Basics (What We’re Fetching)

Before we jump into coding, let’s outline what information we need:

CPU Model & Load (/proc/cpuinfo, top)

Memory Usage (free -m)

Disk Space (df -h)

System Uptime (uptime)

Network Information (ip a, hostname -I)


Step 2: Writing the Script (Fundamentals First)

1️⃣ Shebang & Setup

Start by specifying Bash as the interpreter and clearing the screen:

#!/bin/bash
clear
Enter fullscreen mode Exit fullscreen mode

2️⃣ Fetching CPU Info

Extract the CPU model name using grep on /proc/cpuinfo:

cpu_model=$(grep "model name" /proc/cpuinfo | head -1 | cut -d ':' -f2)
Enter fullscreen mode Exit fullscreen mode

Get the CPU load using top (or uptime for a lightweight alternative):

cpu_load=$(top -bn1 | grep "load average" | awk '{print $10}')
Enter fullscreen mode Exit fullscreen mode

3️⃣ Checking Memory Usage

Use free -m to display RAM usage in MB:

mem_usage=$(free -m | awk 'NR==2{printf "Used: %sMB / Total: %sMB", $3, $2}')
Enter fullscreen mode Exit fullscreen mode

4️⃣ Fetching Disk Space

List available disk space using df -h:

disk_usage=$(df -h | awk '$NF=="/"{printf "Used: %d%%", $5}')
Enter fullscreen mode Exit fullscreen mode

5️⃣ Displaying Network Info

Fetch the current IP address:

ip_address=$(hostname -I | awk '{print $1}')
Enter fullscreen mode Exit fullscreen mode

Step 3: Formatting Output for Readability

Instead of printing raw text, format the output neatly using colors and spacing:

echo -e "\033[32mSystem Information:\033[0m"
echo "-------------------------------"
echo -e "CPU Model: $cpu_model"
echo -e "CPU Load: $cpu_load"
echo -e "Memory: $mem_usage"
echo -e "Disk Space: $disk_usage"
echo -e "IP Address: $ip_address"
Enter fullscreen mode Exit fullscreen mode

🔹 Why it’s useful:

  • Colors improve readability
  • Consistent spacing makes it visually appealing

Step 4: Running the Script

  1. Save the script as sysfetch.sh
  2. Make it executable:
   chmod +x sysfetch.sh
Enter fullscreen mode Exit fullscreen mode
  1. Run it:
   ./sysfetch.sh
Enter fullscreen mode Exit fullscreen mode

📌 Expected Output (Example):

System Information:
-------------------------------
CPU Model: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
CPU Load: 0.78
Memory: Used: 3800MB / Total: 16000MB
Disk Space: Used: 42%
IP Address: 192.168.1.10
Enter fullscreen mode Exit fullscreen mode

Step 5: Enhancing the Script

Add Live Updates

Instead of a one-time report, refresh every few seconds:

while true; do
    clear
    ./sysfetch.sh
    sleep 5
done
Enter fullscreen mode Exit fullscreen mode

Save Output to a Log File

Redirect output for future analysis:

./sysfetch.sh | tee system_report.txt
Enter fullscreen mode Exit fullscreen mode

Make It Interactive

Allow users to choose what info to display:

echo "Select an option: [1] CPU [2] Memory [3] Disk [4] All"
read choice

case $choice in
    1) echo "CPU Model: $cpu_model" ;;
    2) echo "Memory: $mem_usage" ;;
    3) echo "Disk Space: $disk_usage" ;;
    4) ./sysfetch.sh ;;
    *) echo "Invalid option" ;;
esac
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Creating a custom system fetch tool is a great way to:

Improve your Bash scripting skills

Understand Linux system commands

Make system monitoring more efficient

If you're a beginner, this project gives you real-world Bash experience. If you're advanced, you can customize it further with more data points.


Want a Quick Reference for Bash Basics?

If you're just getting started with Bash and need a beginner-friendly guide, check out my Bash Scripting Cheat Book:

📌 A quick reference for beginners to understand Bash scripting faster

📌 Covers fundamental concepts like variables, loops, conditionals, and functions

📌 Designed for absolute beginners—no advanced topics covered

👉 Get the Bash Scripting Cheat Book for just $3.99


Discussion: What System Info Do You Track in Your Bash Scripts?

Drop a comment below and share what extra system details you’d add to this script!

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Jetbrains Survey

Calling all developers!

Share your thoughts and get the chance to win a MacBook Pro, an iPhone 16 Pro, or other exciting prizes. Help shape the coding landscape and earn rewards for your contributions!

Take the survey

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️