How to create a todo list in Bash.

How To Create A Todo List In Bash.

Bash is a powerful scripting language that can handle text manipulation and file operations quite well. Here are the basic steps to create a simple to-do list application in Bash:

  1. Create a Bash Script File: Start by creating a new Bash script file using a text editor of your choice. You can name it something like todo.sh. Make sure the file is executable by running chmod +x todo.sh.

  2. Define Variables: Inside your script, define variables to store the to-do list items. You can use an array to store the tasks:

    # Define an array to store tasks
    tasks=()
  3. Add Functions: Create functions to perform various operations on your to-do list, such as adding, viewing, and removing tasks. Here’s a basic structure for your functions:

    # Function to add a task
    add_task() {
        tasks+=("$1")
    }
    
    # Function to view all tasks
    view_tasks() {
        for ((i=0; i<${#tasks[@]}; i++)); do
            echo "$((i+1)). ${tasks[i]}"
        done
    }
    
    # Function to remove a task
    remove_task() {
        unset "tasks[$1-1]"
        tasks=("${tasks[@]}")
    }
  4. Implement the Main Loop: Create a loop that will allow users to interact with your to-do list. You can use a select loop for a menu-based interface:

    while true; do
        echo "To-Do List"
        echo "1. Add Task"
        echo "2. View Tasks"
        echo "3. Remove Task"
        echo "4. Exit"
        read -p "Select an option: " choice
    
        case $choice in
            1)
                read -p "Enter a new task: " new_task
                add_task "$new_task"
                ;;
            2)
                view_tasks
                ;;
            3)
                read -p "Enter the task number to remove: " task_number
                remove_task "$task_number"
                ;;
            4)
                echo "Goodbye!"
                exit 0
                ;;
            *)
                echo "Invalid option. Please try again."
                ;;
        esac
    done
  5. Run the Script: Save your script and run it in your terminal by executing ./todo.sh. You can now interact with your simple to-do list application.