They say that an image is worth a thousand words, that is why before I explain something, I will show you what is the result of the command that I will put later:
It is something that is updated in real time, that is, every second that information is updated, and it will always be in the upper right corner of the terminal.
This is something useful because, we can be editing a file with nano or vi, we can be installing any type of service or managing whatever, and we will not need to stop what we do, execute date in the terminal to know the date or time With this tip that I show you we will always keep it in mind.
To achieve this in a terminal let's put the following:
while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &
This as such is not a simple command or instruction, but rather a union of them ... come on, a script could also be. Explaining it is a bit complex, however I will do my best 🙂
- while sleep 1; do : This means that every second the following will be executed
- tput sc : It means that the current position will be saved, that is, the position of what will come next will be saved, it will not be necessary to specify it after once.
- put cup 0$ (($ (tput cols) -29)) : This may seem complicated however, it is not that difficult. Basically this is what the position says, that is, the upper right corner. The cup parameter specifies the vertical space that will exist, since we put 0 so it means "just above, at the top". Once the vertical position is defined, we can see the horizontal position, which is taken care of by the rest of the parameters, which to put it simply ... calculates the existing columns and makes sure that it is right on the right edge. If you wish, change the number 29 for others and you will notice the difference.
- data : Well this is simple, date shows us the information we see ... day, month, hour, etc.
- tput rc : They are tput sc we save the position, now with tput rc we restore it.
- done : Here we finish everything, what we started with the while.
As you can see, the terminal is undoubtedly a wonderful space, if a command does not do exactly what we want ... we can join several of them and achieve what we want. See each command as a tool, a tool (hammer) cannot make us a beautiful statue, however, by joining this tool (hammer) with others (wood and chisel) we can reach the dream result 🙂
Oh, by the way ... if you want this to always appear in the terminal without having to execute it every time you open a console, you must put it in .bashrc, that is:
echo "while sleep 1;do tput sc;tput cup 0 \$((\$(tput cols)-29));date;tput rc;done &" >> $HOME/.bashrc
Then if you want to remove it, run the following:
sed -i "s/while sleep 1/#while sleep 1/" $HOME/.bashrc
Well, nothing more to add, I hope it has been useful to you
echo "while sleep 1; do tput sc; tput cup 0 \ $ ((\ $ (tput cols) -29)); date; tput rc; done &" >> $ HOME / .bashrc