unread emails

Terminal Gmail unread count

While configuring my BAR (Bar Ain’t Recursive) for my latest tiling WM installation, Herbstluftwm I wanted a Gmail unread count to display all of the emails that required my attention.

Searching around the web I found this little line of code which provides the output of both the sender and the subject of the unread emails in your inbox.

curl -u username@gmail.com:password --silent "https://mail.google.com/mail/feed/atom" | perl -ne 'print "\t" if //; print "$2\n" if /<(title|name)>(.*)<\/\1>/;'

(If the colon and password part :password is left out then you will be asked for the password, which is probably the safest option, however for my needs not practical).

Within a small bash script this code could then be piped into the bash wc command with the option -l to count the number lines produced.

Setting this equal to a variable count and then subtracting 1, due to the first line being your email address, and then dividing by two for the two lines containing subject and sender for each single message, provides you with the unread count.

count=$(expr $count - 1)
count=$(expr $count / 2)

For me this output was piped into my bar and told to check for new mail every 300 seconds or so.

Voila! A Gmail unread count that can very easily be used as a simple terminal bash script, or piped into something such as my usage.