Command line tools are the backbone of productivity for developers, system administrators, and DevOps engineers. From automating repetitive tasks to managing servers efficiently, a well built CLI tool can save hours of manual work.
If you’ve ever wanted to build your own CLI tool using Bash, this guide will walk you through the process step by step. By the end of this article, you’ll understand how to create a functional, reusable, and user friendly command line tool that you can use in real world projects.
Whether you're working on Linux automation, DevOps workflows, or personal productivity tools, this tutorial is designed to help you level up.
Before diving into the code, let’s understand why Bash is still a powerful choice for CLI tools.
1. Native to Most Systems
Bash comes pre installed on most Unix based systems, including Linux and macOS. This makes your CLI tool portable and easy to distribute.
2. Perfect for Automation
Bash excels at:
- File manipulation
- Process management
- System monitoring
- Task automation
3. Lightweight and Fast
Unlike heavier programming environments, Bash scripts are quick to write and execute with minimal setup.
What You’ll Build
In this tutorial, we’ll create a simple CLI tool called:
devtool
This tool will:
- Accept command line arguments
- Support multiple commands (like
greet, cleanup)
- Provide a help menu
- Handle errors gracefully
Step 1: Create Your Script File
Start by creating a new Bash script:
touch devtool.sh
chmod +x devtool.sh
Add the shebang at the top:
#!/bin/bash
This tells the system to execute the script using Bash.
Step 2: Add a Basic Structure
A good CLI tool follows a clear structure.
#!/bin/bash
# Tool Name: devtool
# Description: A simple CLI tool for automation tasks
show_help() {
echo "Usage: devtool [command]"
echo ""
echo "Commands:"
echo " greet Print a greeting message"
echo " cleanup Remove temporary files"
echo " help Show this help message"
}
This creates a help function, which is essential for usability and self documentation.
Step 3: Handle Commands
Now let’s process user input using a case statement.
COMMAND=$1
case $COMMAND in
greet)
echo "Hello, welcome to devtool!"
;;
cleanup)
echo "Cleaning up temporary files..."
rm -rf /tmp/*
echo "Cleanup completed."
;;
help|--help|-h)
show_help
;;
*)
echo "Unknown command: $COMMAND"
show_help
;;
esac
Why this matters
case makes your CLI tool clean and scalable
- Easy to add new commands later
- Improves readability compared to multiple
if statements
Step 4: Accept Arguments
Let’s make the greet command more dynamic by accepting a name.
greet_user() {
NAME=$1
if [ -z "$NAME" ]; then
echo "Please provide a name."
exit 1
fi
echo "Hello, $NAME! Welcome to devtool."
}
Update your case block:
greet)
greet_user $2
;;
Now you can run:
./devtool.sh greet Gift
Step 5: Improve Error Handling
A professional CLI tool should handle errors gracefully.
Example:
if [ $# -eq 0 ]; then
echo "No command provided."
show_help
exit 1
fi
This ensures users always know what went wrong.
Step 6: Make It Globally Accessible
To use your CLI tool anywhere, move it into a directory in your system’s PATH.
mv devtool.sh /usr/local/bin/devtool
Now you can run:
devtool greet Gift
from anywhere on your system.
Step 7: Add Colors for Better UX (Optional)
Make your CLI tool more engaging with colored output.
GREEN='\033[0;32m'
NC='\033[0m'
echo -e "${GREEN}Operation successful!${NC}"
This improves readability, especially for logs and feedback messages.
As your CLI tool grows, structure it properly:
devtool/
├── devtool.sh
├── commands/
│ ├── greet.sh
│ └── cleanup.sh
└── utils/
This makes your project:
- Scalable
- Maintainable
- Easier to collaborate on
You can extend your CLI tool to handle real world tasks like:
- Deploying applications
- Monitoring server health
- Automating backups
- Managing Docker containers
- Running cron jobs
For developers working with Linux, VPS hosting, or DevOps pipelines, Bash CLI tools are incredibly valuable.
To ensure your tool is professional and reliable:
- Use meaningful function and variable names
- Always include a help command
- Validate user input
- Avoid hardcoding sensitive values
- Log important actions
- Keep scripts modular
Final Thoughts
Creating a CLI tool using Bash is one of the fastest ways to improve your productivity as a developer. With just a few lines of code, you can automate tasks, simplify workflows, and build tools tailored to your needs.
The beauty of Bash lies in its simplicity. You don’t need complex frameworks or heavy dependencies, just a clear idea and good structure.
Start small, keep improving, and before long, you’ll have a collection of powerful CLI tools that make your development process faster and more efficient.
If you found this guide helpful, share it with fellow developers and explore more tutorials on Bash scripting, DevOps, and automation. The more you build, the better you get.