【问题标题】:Variable variable assignment error -"command not found"变量变量赋值错误-“找不到命令”
【发布时间】:2015-07-23 10:24:41
【问题描述】:

当我运行脚本时,出现以下错误。请问我在这里做错了什么?任何帮助表示赞赏 - Bash 新手

错误:

line 12: 0=1: command not found
line 13: 0=1: command not found

我的脚本:

count_raw=0
avg_raw=0

$count_raw=1
$avg_raw=1

echo "count_raw=$count_raw"
echo "avg_raw=$avg_raw"

【问题讨论】:

  • 去掉count_rawavg_raw之前的美元
  • 解决了,谢谢-

标签: bash


【解决方案1】:

= 是一个免费的赋值运算符,$ 持有变量的值(不仅在美国,在 bash 中也是如此)。

因此,当您说:$var=1 时,您实际上是在尝试在 bash 中键入随机字符串(在您的情况下为 0=1),而 bash 不喜欢这样。请看下面的单行代码,它显示了一个示例,您可以在其中输入 $var=1 并且 bash 将能够处理它:

var=1; if [[ $var=1 ]]; then printf "Congrats! You have learned the difference between variable assignment and variable comparison in the ${var}st attempt.\n"; fi;

【讨论】:

  • 很好,很有趣,还回答了 T 的问题。谢谢 Donbhupi
猜你喜欢
  • 2011-01-17
  • 2012-12-12
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
相关资源
最近更新 更多