CPU temperature embedded in Bash command prompt

Yes, it is possible, but the details depend on your system. In most cases, the command sensors should show it.

  1. Install the necessary package

    sudo apt-get install lm-sensors
    
  2. Run sensors-detect and follow the prompts

    sudo sensors-detect
    
  3. Install any extra drivers if sensors-detect tells you to.

  4. Run sensors to make sure it works

    $ sensors
    acpitz-virtual-0
    Adapter: Virtual device
    temp1:        +27.8°C  (crit = +110.0°C)
    temp2:        +29.8°C  (crit = +110.0°C)
    
    coretemp-isa-0000
    Adapter: ISA adapter
    Physical id 0:  +63.0°C  (high = +105.0°C, crit = +105.0°C)
    Core 0:         +62.0°C  (high = +105.0°C, crit = +105.0°C)
    Core 1:         +63.0°C  (high = +105.0°C, crit = +105.0°C)
    
    nct6776-isa-0a00
    Adapter: ISA adapter
    Vcore:                  +1.86 V  (min =  +0.00 V, max =  +1.74 V)  ALARM
    in1:                    +1.36 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    AVCC:                   +3.33 V  (min =  +2.98 V, max =  +3.63 V)
    +3.3V:                  +3.33 V  (min =  +2.98 V, max =  +3.63 V)
    in4:                    +1.01 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    in5:                    +0.00 V  (min =  +0.00 V, max =  +0.00 V)
    in6:                    +0.21 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
    3VSB:                   +3.31 V  (min =  +2.98 V, max =  +3.63 V)
    Vbat:                   +3.18 V  (min =  +2.70 V, max =  +3.63 V)
    fan1:                     0 RPM  (min =    0 RPM)
    fan2:                  3292 RPM  (min =    0 RPM)
    SYSTIN:                  +0.0°C  (high =  +0.0°C, hyst =  +0.0°C)  sensor = thermistor
    CPUTIN:                 +51.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = CPU diode
    AUXTIN:                  +0.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = CPU diode
    PCH_CHIP_CPU_MAX_TEMP:  +58.0°C  (high = +80.0°C, hyst = +75.0°C)
    PECI Agent 0:           +60.0°C  (high = +80.0°C, hyst = +75.0°C)
                                     (crit = +105.0°C)
    PCH_CHIP_TEMP:           +0.0°C  
    PCH_CPU_TEMP:            +0.0°C  
    intrusion0:            OK
    intrusion1:            OK
    beep_enable:           disabled
    
  5. Parse the output to get only the CPU temperature.

    As you can see above, the output on my system is different than yours. However, the line we care about here is the same. You can get the CPU temperature with:

    $ sensors | grep -oP 'Physical.*?\+\K[0-9.]+'
    63.0
    
  6. Edit your ~/.bashrc (or equivalent file if you're using another shell) and add a function that runs the command above:

    show_temp(){
        sensors | grep -oP 'Physical.*?\+\K[0-9.]+'
    }
    
  7. Use the function in your prompt. For example:

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

Install lm-sensors:

sudo apt-get install lm-sensors

Detect what sensors are available:

sudo sensors-detect

To show the temperature:

sensors

Tags:

Bash