I am trying to get two sensors and add both answers to variable in a bash script.

I got this but it is not suited to my needs.

[root@**** ~]# sensors
Adapter: ISA adapter
Core 0:       +40.0°C  (high = +80.0°C, crit = +96.0°C)
Core 1:       +45.0°C  (high = +80.0°C, crit = +96.0°C)
Core 2:       +50.0°C  (high = +80.0°C, crit = +96.0°C)
Core 8:       +47.0°C  (high = +80.0°C, crit = +96.0°C)
Core 9:       +48.0°C  (high = +80.0°C, crit = +96.0°C)
Core 10:      +45.0°C  (high = +80.0°C, crit = +96.0°C)

Adapter: ISA adapter
Core 0:       +36.0°C  (high = +80.0°C, crit = +96.0°C)
Core 1:       +44.0°C  (high = +80.0°C, crit = +96.0°C)
Core 2:       +41.0°C  (high = +80.0°C, crit = +96.0°C)
Core 8:       +42.0°C  (high = +80.0°C, crit = +96.0°C)
Core 9:       +44.0°C  (high = +80.0°C, crit = +96.0°C)
Core 10:      +48.0°C  (high = +80.0°C, crit = +96.0°C)

[root@*** ~]# sensors | sed -rn 's/.*Core 0:\s+.([0-9]+).*/\1/p'

I need 40 and 36 saved into variables. Is there anyway to add them in two separate variables in a bash script?

  • Maybe this gives you an idea var=$(awk '/Core 0:/{printf("%s ", $3+0)}' sensors ) – Valentin Bajrami Nov 6 '18 at 10:28

You could also bypass the sensors command and read the values directly from the /sys filesystem:

VAR1RAW=$(cat /sys/devices/platform/coretemp.0/hwmon/hwmon*/temp1_input)
VAR1=$(expr $VAR1RAW / 1000)
VAR2RAW=$(cat /sys/devices/platform/coretemp.1/hwmon/hwmon*/temp1_input)
VAR2=$(expr $VAR2RAW / 1000)

(The asterisk at the hwmon* path element allows us to ignore the hwmon device number: as we're coming in through the .../platform/coretemp.N/... route, there'll be just one matching directory at that level.)

The raw values in the /sys filesystem are expressed in thousandths of degrees, so you'll need to divide by 1000. Since expr only does integer arithmetic, that'll also drop out the decimals at the same time.


This all could be achieved using a simple awk one liner only. For example:

awk '/Core 0:/{i[$3+0];} END { n=1; for(a in i) printf("%s=%d\n", "var"n++,a)}'
  • Match pattern /Core 0/
  • Create an array variable i[$3+0]. The +0 will strip everything after the digit
  • Initialize variable n to 1 as in: n=1. Loop trough indices and print the results by incrementing variable n.

NOTE! By convention, environment variables (PATH, EDITOR, SHELL, ...) and internal shell variables (BASH_VERSION, RANDOM, ...) are fully capitalized. All other variable names should be lowercase. Since variable names are case-sensitive, this convention avoids accidentally overriding environmental and internal variables.


You can try something like:

VAR=$(sensors | sed -rn 's/.*Core 0:\s+.([0-9]+).*/\1/p'|tr '\n' ' ')

This will replace newline with space and add both values to the VAR variable. If you want to separate them in two variables you can use the code below (I know its not optimal, but work for me)

VAR1=$(echo $VAR|awk '{print $1}')
VAR2=$(echo $VAR|awk '{print $2}')
  • is there a way to sperate the vars like VAR1=36 and VAR2=40 – Wayno Nov 6 '18 at 6:56
  • @Wayno, please check my edited answer – Romeo Ninov Nov 6 '18 at 7:03
  • 1
    really that easy, lol i was racking my brain over it, Thank you for your time. – Wayno Nov 6 '18 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.