If you’re curious what temperature your Raspberry Pi is at, but don’t feel like running a constant temperature monitor, you can modify your prompt to display this dynamically. This is a very small modification to your .bashrc file, which should only take a minute.
After this guide, your prompt will look like this:
Follow these commands to change the prompt:
First, make a backup!
cp ~/.bashrc ~/.bashrc.bak
Next, edit the .bashrc file. You can do this with any editor like vi (or even through something like WinSCP and Notepad++). Nano is the easiest for most beginners to use, so this guide will be utilizing that:
nano ~/.bashrc
Scroll down through the file until you find this line:
You can find this also by simply using ctrl+w and searching for PS1.
Delete the whole line, and replace it with the following:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] `/opt/vc/bin/vcgencmd measure_temp | cut -d = -f 2` \[\033[01;34m\]\w \$\[\033[00m\] '
Afterwards type ctrl+o and press Enter to save the file. Exit the file with ctrl+x.
Your changes will be live in a new bash session, so either type “bash” to open a new shell, or type exit and open a new session.
What to do if you encounter “VCHI initialization failed”
To be able to run this command, your user needs to be a member of the video group. You can add your user to the video group with the following command:
usermod -a -G video <username>
The standard user pi is already a member of this group.
How does this work?
The following command displays the current temperature on a Raspberry Pi:
/opt/vc/bin/vcgencmd measure_temp
This outputs the following:
temp=46.2'C
Seeing as we don’t like the “temp=” part of this, as it’s not very useful information, we want to remove it. There are many ways to do this, I find the easiest here to use the “cut” command which cuts input into fields.
/opt/vc/bin/vcgencmd measure_temp | cut -d = -f 2
The -d is the delimiter, which means we’ll be splitting on every “=“. The -f is the fields option, meaning we only want the second field returned. By adding this to the .bashrc file between backticks (`) it will execute on every enter.
If you have any questions or remarks please leave a comment below!
Leave a Reply