Bash变量分配无法正常工作 [英] Bash variable assignment not working expected

查看:68
本文介绍了Bash变量分配无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

status=0
$status=1

echo $status

有人可以告诉我我在做什么错吗?

Can anyone tell my what i am doing wrong with this?

它给了我以下错误:

 0=1: command not found

推荐答案

此行可以-将值0分配给变量status:

This line is OK - it assigns the value 0 to the variable status:

status=0

这是错误的:

$status=1

通过在变量名之前放置$即可取消引用它,即获取其值,在这种情况下为0.换句话说,bash扩展了您写的内容:

By putting a $ in front of the variable name you are dereferencing it, i.e. getting its value, which in this case is 0. In other words, bash is expanding what you wrote to:

0=1

没有意义,因此是错误.

Which makes no sense, hence the error.

如果您打算将新值1重新分配给状态变量,则只需执行与原始分配相同的操作即可:

If your intent is to reassign a new value 1 to the status variable, then just do it the same as the original assignment:

status=1

这篇关于Bash变量分配无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆