shell 变量前带1,例如1min变量为什么echo输出报错

2025-05-08 15:17:46
推荐回答(1个)
回答1:

bash的变量名是字母数字和下划线的组合,而且必须以字母或下划线开头。
你的变量名以数字开头,是不合法的变量,脚本肯定会出错。
改成min1就可以了。
min1=`uptime|awk -F ":" '{print $5}'|awk -F "," '{print $1}'`
echo -e "${min1}"

你可以man bash看下相关说明,这里摘录一段:
DEFINITIONS
...
name A word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an identifier.