Prerequisites
- Basic knowledge of Bash commands
- Understanding of variables in Bash
- Familiarity with conditional statements and
case
statements - Ability to create and manage
text files
- No additional packages are required for this simple script
DID YOU KNOW?
The first to-do list is credited to the American author Mark Twain, who famously said, “Eat a live frog first thing in the morning and nothing worse will happen to you the rest of the day!”
The Script
This Bash script leverages simple text file operations to create a to-do list. You can add, list, and remove tasks effortlessly. Here’s the script:
#!/bin/bash
TODO_FILE="$HOME/todo.txt"
function show_help {
echo "Usage: todo.sh [command] [task]"
echo "Commands:"
echo " add [task] Add a task"
echo " list List all tasks"
echo " remove [task] Remove a task"
}
function add_task {
if [[ -z "$1" ]]; then
echo "Error: Task cannot be empty."
exit 1
fi
echo "$1" >> "$TODO_FILE"
echo "Added task: $1"
}
function list_tasks {
if [[ -s "$TODO_FILE" ]]; then
echo "Your To-Do List:"
cat "$TODO_FILE"
else
echo "No tasks found."
fi
}
function remove_task {
if [[ -z "$1" ]]; then
echo "Error: Task to remove cannot be empty."
exit 1
fi
if [[ ! -f "$TODO_FILE" ]]; then
echo "No tasks found to remove."
exit 1
fi
# Escape task for `sed` to handle spaces and special characters.
escaped_task=$(printf '%s\n' "$1" | sed 's/[]\/$*.^|[]/\\&/g')
# Use `sed` to remove the exact matching line.
sed -i "/^${escaped_task}$/d" "$TODO_FILE"
echo "Removed task: $1"
}
case "$1" in
add)
add_task "$2"
;;
list)
list_tasks
;;
remove)
remove_task "$2"
;;
*)
show_help
;;
esac
Step-by-Step Explanation
NOTE!
Before running the script, ensure that the script has the correct permissions to execute. You can set this by running chmod +x todo.sh
.
This script has four primary functions: showing help, adding a task, listing tasks, and removing tasks. Each function is designed to handle specific actions related to your to-do list.
- Setting Up the Environment: The script begins by declaring the location of your to-do file stored in your home directory.
- Displaying Help: If the user inputs an invalid command, the
show_help
function provides usage instructions. - Adding Tasks: The
add_task
function appends a new task to the to-do file. - Listing Tasks: The
list_tasks
function displays all existing tasks or alerts the user if there are none. - Removing Tasks: The
remove_task
function allows users to delete a specific task from the list usinggrep
.
How to Run the Script
To execute your to-do list script, follow these steps:
- Open your terminal.
- Navigate to the directory where you saved
todo.sh
. - Run the script using one of the commands:
./todo.sh add "Your Task"
,./todo.sh list
, or./todo.sh remove "Your Task"
.
Output
Conclusion
Creating a simple to-do list with a Bash script is an excellent way to boost your productivity while practicing your scripting skills. This straightforward script can be expanded and modified to suit your personal workflow needs. Experiment with adding new features, such as deadlines or priorities, to enhance your to-do list further!
FAQ
-
Can I store my to-do list in another directory?
Yes, simply change the path assigned to
TODO_FILE
to your desired location. -
What if I want to keep my tasks in a different file format?
You can modify the script to write to a different file type, but ensure you adjust the reading and writing logic accordingly.
-
Is it possible to add due dates to tasks?
Absolutely! You can expand the
add_task
function to include a due date as part of the task description. -
What if I forget the script commands?
The script includes a help function that you can access by running
./todo.sh
without any arguments.