DEV Community

Cover image for Build a Simple TODO List with Shell Script 🐚
Ernane Ferreira
Ernane Ferreira

Posted on

Build a Simple TODO List with Shell Script 🐚

Managing tasks directly from the terminal can be a breeze with the power of shell scripting! In this post, I'll walk you through creating a simple yet functional TODO list using a few shell functions. We'll store tasks in a text file, add unique identifiers to each item, and provide commands for adding, removing, and clearing tasks.

Let’s dive in! πŸš€

Prerequisites βš™οΈ

You'll need a terminal using bash or zsh. The following functions can be added to your ~/.zshrc or ~/.bashrc file to make them available as commands in your terminal.

Step 1: Set Up Your Storage 🎲

First, let’s create an environment variable that defines where your tasks will be stored. Add this line to your configuration file:

export TODO="${HOME}/todo.txt"
Enter fullscreen mode Exit fullscreen mode

This will store your task list in a file called todo.txt located in your home directory.

Step 2: Define Your Functions ✨

Now let's add some shell functions to manage our TODO list:

tla() { [ $# -eq 0 ] && cat $TODO || (echo "$(echo $* | md5sum | cut -c 1-4) πŸ‘‰πŸΌ $*" >> $TODO && cat $TODO) ;}
tlr() { sed -i '' "/^$*/d" $TODO && cat $TODO ;}
tl() { cat $TODO ;}
tlc() { cat /dev/null > $TODO ;}
Enter fullscreen mode Exit fullscreen mode

Here’s what each function does:

  • tla (Todo List Add): Adds tasks to the list. It generates a 4-character md5 hash to serve as a unique identifier for each task. If no argument is passed, it displays the current task list.
  • tlr (Todo List Remove): Removes tasks from the list by matching their md5 identifier.
  • tl (Todo List): Simply displays the current list.
  • tlc (Todo List Clear): Clears the entire task list, removing all entries at once.

At the end of each operation, the current state of your TODO list will be displayed.

Step 3: See It in Action β˜•οΈ

Here’s a quick demonstration of how these commands work:

➜  ~ tl       
➜  ~ tla test 0
# 0e57 πŸ‘‰πŸΌ test 0
➜  ~ tla test 1
# 0e57 πŸ‘‰πŸΌ test 0
# 2490 πŸ‘‰πŸΌ test 1
➜  ~ tla test 2
# 0e57 πŸ‘‰πŸΌ test 0
# 2490 πŸ‘‰πŸΌ test 1
# b0b3 πŸ‘‰πŸΌ test 2
➜  ~ tlr 2490
# 0e57 πŸ‘‰πŸΌ test 0
# b0b3 πŸ‘‰πŸΌ test 2
➜  ~ tl
# 0e57 πŸ‘‰πŸΌ test 0
# b0b3 πŸ‘‰πŸΌ test 2
➜  ~ tlc
➜  ~ tl
Enter fullscreen mode Exit fullscreen mode
  • tla test 0: Adds "test 0" to the list with an md5 identifier.
  • tlr 2490: Removes the task with the identifier 2490 (in this case, "test 1").
  • tlc: Clears the list.

Conclusion ✍️

With just a few lines of shell script, you now have a functional TODO list right in your terminal. You can easily add, remove, or clear tasks with simple commands. This setup is lightweight and can be modified to fit your workflow.

Feel free to extend these functions or add your own twists. Happy hacking! πŸš€

Top comments (18)

Collapse
 
slimgee profile image
Given Ncube

Bash has always been this mystery to me, every time I need a script I would reach for PHP or Ruby

Collapse
 
ernanej profile image
Ernane Ferreira

I completely understand you! I often feel the same way. Bash isn’t my main language, but I enjoy challenging myself from time to time. When it's something urgent, of course, I’ll turn to a tool or language I’m more comfortable with. However, whenever possible, it's worth stepping out of your comfort zone and trying something new. Sometimes, in those moments, you might gain insights from a language you're less familiar with, which you can even apply to your primary one. If you have the time β€” and sometimes the sanity β€” give it a try!

Collapse
 
slimgee profile image
Given Ncube

that's cool

Collapse
 
josephj11 profile image
Joe

Bash is pretty easy with one major exception. When your command includes any tokens that bash recognizes, it will attempt to perform several types of expansions to it. It does this iteratively until nothing else will expand.

When you take advantage of this on purpose, it's great, but getting it to leave something the way it was using quoting/escaping can be quite challenging. You have to learn that the hard way - mostly by trial and error. The bash trace flags (-v and -x) help with figuring out how the original text of the script gets expanded.

Also, some people make a variable names uppercase for some reason. I started out that way, but switched to lowercase for everything except environment variables.

Collapse
 
slimgee profile image
Given Ncube

I hear people say bash is great until you need an array

Thread Thread
 
ernanej profile image
Ernane Ferreira

But who needs them? Bash and arrays have a relationship as smooth as trying to split a string without 'awk'

Thread Thread
 
slimgee profile image
Given Ncube

W

Thread Thread
 
josephj11 profile image
Joe

Bash has normal and associative arrays. The syntax can be a bit clunky and it only supports one dimensional arrays, so you have to make arrays of arrays for more dimensions.
I'm a big fan of awk. It's usually way easier than sed whenever you have to do context dependent editing. And you can add print statements to it for viewing partial results... A lot of people ignore cut. It's great for simple things.

Collapse
 
josephj11 profile image
Joe • Edited

That's a perfect way to get a whole new app with almost no effort.
I have a similar one for an activity journal/log.

One slight improvement. tlc() uses what is known as a useless cat. It can be:

tlc() { > "$TODO" ;}
Enter fullscreen mode Exit fullscreen mode

I also added quotes to defend against things like embedded blanks. Shell variables should usually be quoted unless there's specific reason not to.

Collapse
 
ernanej profile image
Ernane Ferreira

That makes a lot of sense, thanks!

Collapse
 
martinbaun profile image
Martin Baun

Really enjoyed this piece!

Collapse
 
ernanej profile image
Ernane Ferreira

Thanks for that!

Collapse
 
tobidelly profile image
TD!

Nice

Collapse
 
ernanej profile image
Ernane Ferreira

Thanks!

Collapse
 
rickvian profile image
Rickvian Aldi

whoa, TODO list in shell, why not?

Collapse
 
ernanej profile image
Ernane Ferreira

why NOT?! lol

Collapse
 
carloshendvpm profile image
Carlos Henrique

This is way too great, I will definitely use this one on my workspace!

Collapse
 
ernanej profile image
Ernane Ferreira

Hey, I'm excited about this!