在 Bash 变量赋值中找不到命令错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2268104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:56:06  来源:igfitidea点击:

Command not found error in Bash variable assignment

bashshellsyntaxshvariable-assignment

提问by Jake Wilson

I have this script called test.sh:

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

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

when I run sh test.shI get this:

当我跑步时,sh test.sh我得到了这个:

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

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.

我究竟做错了什么?我在网上查看非常基本/初学者的 bash 脚本教程,这就是他们所说的声明变量的方式......所以我不确定我做错了什么。

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

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

回答by William Pursell

You cannot have spaces around your '=' sign.

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

When you write:

当你写:

STR = "foo"

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

bash 尝试使用 2 个参数(字符串“=”和“foo”)运行名为 STR 的命令

When you write:

当你写:

STR =foo

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

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

When you write:

当你写:

STR= foo

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

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

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

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

  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.
  1. 所述第一命令是完全等效于:STR "=" "foo"
  2. 第二个是相同的STR "=foo"
  3. 最后一个等价于STR="" foo.

The relevant section of the sh language spec, section 2.9.1states:

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.

“简单命令”是一系列可选的变量赋值和重定向,以任何顺序,可选地跟随单词和重定向,由控制运算符终止。

In that context, a wordis 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", STRis not a variable assignment.

在这种情况下,aword是 bash 将要运行的命令。任何包含=(在字符串开头以外的任何位置)不是重定向的字符串都是变量赋值,而任何不是重定向且不包含的字符串=都是命令。在STR = "foo",STR不是变量赋值。

回答by Joey

Drop the spaces around the =sign:

去掉=标志周围的空格:

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

回答by Arkapravo

In the interactive mode everything looks fine:

在交互模式下,一切看起来都很好:

$ str="Hello World"
$ echo $str
Hello World

Obviously(!) as Johannes said, no space around =. In case there is any space around =then in the interactive mode it gives errors as

显然(!)正如约翰内斯所说,周围没有空间=。如果=在交互模式下周围有任何空间,它会给出错误

No command 'str' found

未找到命令“str”

回答by Derek Haber

I know this has been answered with a very high-quality answer. But, in short, you cant have spaces.

我知道这已经得到了非常高质量的回答。但是,简而言之,您不能有空格。

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

Didn't work because of the spaces around the equal sign. If you were to run...

由于等号周围的空格而不起作用。如果你要跑...

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

It would work

它会工作

回答by Ravi Solanki

When you define any variable then you do not have to put in any extra spaces.

当您定义任何变量时,您不必输入任何额外的空格。

E.g.

例如

name = "Stack Overflow"  
// it is not valid, you will get an error saying- "Command not found"

So remove spaces:

所以删除空格:

name="Stack Overflow" 

and it will work fine.

它会正常工作。