在Bash变量分配中找不到命令错误 [英] Command not found error in Bash variable assignment

查看:79
本文介绍了在Bash变量分配中找不到命令错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为test.sh的脚本:

I have this script called test.sh:

#!/bin/bash
STR = "Hello World"
echo $STR

当我运行sh test.sh时,我得到了:

when I run sh test.sh I get this:

test.sh: line 2: STR: command not found

我做错了什么?我在网上看了极其基础/初学者的bash脚本教程,这就是他们说的声明变量的方式……所以我不确定自己做错了什么.

What am I doing wrong? I look at extremely basic/beginners bash scripting tutorials online and this is how they say to declare variables... So I'm not sure what I'm doing wrong.

我在Ubuntu Server 9.10上.是的,bash位于/bin/bash.

I'm on Ubuntu Server 9.10. And yes, bash is located at /bin/bash.

推荐答案

'='符号周围不能有空格.

You cannot have spaces around your '=' sign.

撰写时:

STR = "foo"

bash尝试运行带有2个参数(字符串'='和'foo')的名为STR的命令

bash tries to run a command named STR with 2 arguments (the strings '=' and 'foo')

撰写时:

STR =foo

bash尝试运行带有1个参数(字符串'= foo')的名为STR的命令

bash tries to run a command named STR with 1 argument (the string '=foo')

撰写时:

STR= foo

bash尝试在其环境中将STR设置为空字符串的情况下运行命令foo.

bash tries to run the command foo with STR set to the empty string in its environment.

我不确定这是否有助于澄清或仅仅是混淆,但请注意:

I'm not sure if this helps to clarify or if it is mere obfuscation, but note that:

  1. 第一个命令完全等效于:STR "=" "foo"
  2. 第二个与STR "=foo"相同,
  3. 最后一个等价于STR="" foo.
  1. the first command is exactly equivalent to: STR "=" "foo",
  2. the second is the same as STR "=foo",
  3. and the last is equivalent to STR="" foo.

sh语言规范,第2.9.1节的相关部分,声明:

简单命令"是一系列可选的变量分配和重定向,可以按任意顺序进行,并可选地后跟单词和重定向,并由控制操作员终止.

A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.

在这种情况下,word是bash将要运行的命令.包含=的任何字符串(不在字符串开头的任何位置)都不是重定向,而是变量分配,而不是重定向且不包含=的任何字符串都是命令.在STR = "foo"中,STR不是变量分配.

In that context, a word is the command that bash is going to run. Any string containing = (in any position other than at the beginning of the string) which is not a redirection is a variable assignment, while any string that is not a redirection and does not contain = is a command. In STR = "foo", STR is not a variable assignment.

这篇关于在Bash变量分配中找不到命令错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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