How you make stuffs when you’re bored — the making of world’s first tweeting terminal
As it turns out, my experience at HackGFS was pretty tragic. I teamed up, as usual, with my programming mates from Robotics team, but we failed to make anything (that works). Then, at the last minute I came up with an idea to make this Tweeting Terminal.
Basically it’s a twitter bot residing in your terminal, that tweets your command whenever you execute one. And no one, as I found out, had ever came up with this phoolish idea before.
I first consulted Mr. Tesla with his ideas about this, where we both agree that we’ll need to access to Bash’s log of commands. While it seems easy to do — normally the history command doesn’t work in a Bash script as it’s a built-in function of Bash, not an external application located under /usr/bin.
As my first attempt, I tried editing the $PS4 environmental variable as suggested in this Stackexchange post. It didn’t work.
When also hovering over that post, I had a new discovery: the $PROMPT_COMMAND variable.
Basically $PROMPT_COMMAND executes after every command you executes, e.g. if you execute `ls`, while having $PROMPT_COMMAND set to `fortune | cowsay` the result will look something like:
alex ~$ ls
apCompSci.txt eclipse-workspace mendel.sh testQT.ui
bioLab.txt favicon.zip newfile1.lyx UserAccountControl.ui
bio.txt final_dates.txt note.txt workspace
Desktop HelloWorld.java Pictures yersinia.log
Documents history.sh polynomialDecomposition.md
Downloads IdeaProjects schedule
eclipse kickass.txt something
/ <|Rain|> Knghtbrd: let me give you \
| access to the zone files <Knghtbrd> oh |
| gods — you do realize I have never |
| played with bind right? <|Rain|> uhoh |
\ :) /
— — — — — — — — — — — — — — — — — — — —
|| — — w |
Therefore, a very simple one-liner can be produced to print on the screen the last command you executed:
alex ~$ PROMPT_COMMAND=”echo $(history | tail -1)”
Since the history command is being executed in an interactive shell, Bash has no problem with executing it.
The output, therefore, would be:
505 history | tail -1
Note that the ‘505’ number is the number of command versus the total amount of command in history.
To produce a valid output for tweeting, we’ll still need to get rid of the ` 505 ` part of it. This is done using `cut`:
alex ~$ PROMPT_COMMAND='echo $(history | tail -1 | cut -c 8-)'