首页 > 解决方案 > Bash如何将linux命令的结果存储在变量中?

问题描述

我正在尝试将计算机的温度存储在变量中。我试过这个但它不起作用:

#!/bin/bash
temp = cat "/sys/class/thermal/thermal_zone0/temp"
echo "$temp"

我也试过这个:

#!/bin/bash
temp = $(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"

但没有任何效果,它总是说

./temp.sh: line 2: temp: command not found

标签: linuxbash

解决方案


空间很关键!这工作正常:

# NO space around `=`
temp=$(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"

推荐阅读