Software & AppsOperating SystemLinux

Embedding CPU Temperature in Bash Command Prompt with Grep

Ubuntu 20

In this article, we are going to explore how to embed the CPU temperature into your Bash command prompt using the grep function. This can be a useful tool for system administrators who want to monitor their system’s temperature directly from the command line.

Quick Answer

Yes, you can embed the CPU temperature in your Bash command prompt using the grep function. By using lm-sensors to detect and monitor the sensors on your Linux system, and then using grep to extract the CPU temperature from the sensor output, you can display the temperature directly in your command prompt.

Prerequisites

Before we start, you need to have a Linux system with Bash shell and lm-sensors package installed. If you haven’t installed lm-sensors, you can do so by running the following command:

sudo apt-get install lm-sensors

This command uses the apt-get package handling utility in Ubuntu to install lm-sensors. The sudo command is used to run this command with root privileges.

Detecting Sensors

After installing lm-sensors, you need to detect the available sensors on your system. You can do this by running the following command:

sudo sensors-detect

This command will prompt you to answer a series of questions. You can simply press Enter to accept the default answers. If any additional drivers are required, the utility will prompt you to install them.

Checking Sensor Output

You can check if lm-sensors is working properly by running the sensors command:

sensors

This command will display the output of all available sensors, including the CPU temperature.

Extracting CPU Temperature

To extract the CPU temperature from the sensors output, you can use the grep function. The grep function is a powerful tool in Linux used for searching and filtering text. Here is an example of how to use it:

sensors | grep -oP 'Physical.*?\+\K[0-9.]+'

This command uses a regular expression to match the line that contains the CPU temperature and extracts the temperature value. The -o option tells grep to only output the part of the line that matches the regular expression, and the -P option enables Perl-compatible regular expressions.

Embedding CPU Temperature in Command Prompt

To display the CPU temperature in your command prompt, you need to add a function to your ~/.bashrc file. This file is a script that is executed whenever a new terminal session is started in interactive mode. Here is an example of how to add the function:

show_temp(){
 sensors | grep -oP 'Physical.*?\+\K[0-9.]+'
}

This function runs the command to extract the CPU temperature and returns the result.

After adding the function, you need to modify the PS1 variable in your ~/.bashrc file. The PS1 variable defines the primary prompt string which is displayed whenever Bash is ready to read a command. Here is an example of how to modify it:

PS1="\u@\h (\$(show_temp)) $ "

This command prompt will display the username, hostname, and CPU temperature in parentheses.

Conclusion

In this article, we have explored how to embed the CPU temperature into your Bash command prompt using the grep function. This can be a useful tool for system administrators who want to monitor their system’s temperature directly from the command line. Remember to adjust the commands according to your system and needs.

How can I check if `lm-sensors` is installed on my system?

To check if lm-sensors is installed on your system, you can run the following command in your terminal:

sensors --version

If lm-sensors is installed, it will display the version number. If it is not installed, you will see an error message.

Can I use this method to monitor the CPU temperature on a non-Linux system?

No, this method is specific to Linux systems. lm-sensors is a package designed for Linux distributions, and the commands used in this article are specific to Linux. However, there are similar tools available for other operating systems that can be used to monitor CPU temperature.

Leave a Comment

Your email address will not be published. Required fields are marked *