Bash 变量赋值中出现命令未找到错误

发布于 2024-08-21 19:33:39 字数 352 浏览 10 评论 0原文

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

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

当我运行 sh test.sh 时,我得到以下信息:

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

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

我使用的是 Ubuntu Server 9.10。是的,bash 位于 /bin/bash

I have this script called test.sh:

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

when I run sh test.sh I get this:

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

彩扇题诗 2024-08-28 19:33:39

= 符号周围不能有空格。

当您编写时:

STR = "foo"

bash 尝试运行一个名为 STR 的命令,该命令带有 2 个参数(字符串 =foo

当您编写时:

STR =foo

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

当您编写:

STR= foo

bash 尝试运行命令 foo,并将 STR 设置为其环境中的空字符串。

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

  1. 第一个命令完全等同于: STR "=" "foo"
  2. 第二个命令与STR "=foo"
  3. 最后一个相当于STR="" foo

sh 语言规范第 2.9.1 节的相关部分状态:

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

在这种情况下,一个单词就是 bash 将要运行的命令。任何包含 = 的字符串(在字符串开头以外的任何位置),该字符串不是重定向,并且其中 = 之前的字符串部分是有效的变量名是变量赋值,而任何不是重定向或变量赋值的字符串都是命令。在 STR = "foo" 中,STR 不是变量赋值。

You cannot have spaces around the = sign.

When you write:

STR = "foo"

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

When you write:

STR =foo

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

When you write:

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. 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.

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

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 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 and in which the portion of the string before the = is a valid variable name is a variable assignment, while any string that is not a redirection or a variable assignment is a command. In STR = "foo", STR is not a variable assignment.

怀中猫帐中妖 2024-08-28 19:33:39

删除 = 符号周围的空格:

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

Drop the spaces around the = sign:

#!/bin/bash 
STR="Hello World" 
echo $STR 
掐死时间 2024-08-28 19:33:39

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

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

显然(!)正如 Johannes 所说,= 周围没有空格。如果 = 周围有任何空格,那么在交互模式下它会给出错误:

未找到命令“str”

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

尘世孤行 2024-08-28 19:33:39

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

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

由于等号周围有空格,所以不起作用。如果你要跑...

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

它会起作用

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

清醇 2024-08-28 19:33:39

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

例如

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

,删除空格:

name="Stack Overflow" 

它会正常工作。

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.

行至春深 2024-08-28 19:33:39

要添加到接受的答案 - 在变量名称中使用破折号将给出相同的错误。删除 - 以消除错误。

STR-STR="Hello World"
bash: STR-STR=Hello World: command not found

To add to the accepted answer - using dashes in your variable name will give the same error. Remove - to get rid of the error.

STR-STR="Hello World"
bash: STR-STR=Hello World: command not found
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文