Bash Script as a Simple To-Do List

Creating a simple to-do list is a common task that can be accomplished using various programming languages. One straightforward option is to utilize a Bash script, which allows users to create, view, and manage tasks right from the command line. This article will guide you through setting up a simple to-do list using a Bash script.

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.

  1. Setting Up the Environment: The script begins by declaring the location of your to-do file stored in your home directory.
  2. Displaying Help: If the user inputs an invalid command, the show_help function provides usage instructions.
  3. Adding Tasks: The add_task function appends a new task to the to-do file.
  4. Listing Tasks: The list_tasks function displays all existing tasks or alerts the user if there are none.
  5. Removing Tasks: The remove_task function allows users to delete a specific task from the list using grep.

How to Run the Script

To execute your to-do list script, follow these steps:

  1. Open your terminal.
  2. Navigate to the directory where you saved todo.sh.
  3. Run the script using one of the commands: ./todo.sh add "Your Task", ./todo.sh list, or ./todo.sh remove "Your Task".

Output

Managing To-Do list with bash script
Managing To-Do list with bash script

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

  1. Can I store my to-do list in another directory?

    Yes, simply change the path assigned to TODO_FILE to your desired location.

  2. 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.

  3. 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.

  4. What if I forget the script commands?

    The script includes a help function that you can access by running ./todo.sh without any arguments.