找不到 Bash 变量赋值和命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1714603/
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:40:51  来源:igfitidea点击:

Bash variable assignment and command not found

bashshellvariablescygwin

提问by CJ.

I have a shell script that will let me access global variables inside the script, but when I try to create my own, it responds with: command not found.

我有一个 shell 脚本,可以让我访问脚本中的全局变量,但是当我尝试创建自己的全局变量时,它会响应:找不到命令。

#!/bin/bash
J = 4
FACE_NAME = "eig$J.face"
USER_DB_NAME = "base$J.user"

When I run the above script I get:

当我运行上面的脚本时,我得到:

./test1.sh line 2: J: command not found
./test1.sh line 3: FACE_NAME: command not found
./test1.sh line 4: USER_DB_NAME: command not found

Any ideas?? I'm using Cygwin under Windows XP.

有任何想法吗??我在 Windows XP 下使用 Cygwin。

回答by Andrew Hare

Try this (notice I have removed the spaces from either side of the =):

试试这个(注意我已经删除了 两侧的空格=):

#!/bin/bash
J="4"
FACE_NAME="eig$J.face"
USER_DB_NAME="base$J.user"

Bash doesn't like spaces when you declare variables - also it is best to make every value quoted (but this isn't as essential).

当你声明变量时,Bash 不喜欢空格 - 最好让每个值都被引用(但这不是必需的)。

回答by Paused until further notice.

It's a good idea to use braces to separate the variable name when you are embedding a variable in other text:

在其他文本中嵌入变量时,最好使用大括号分隔变量名称:

#!/bin/bash
J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"

The dot does the job here for you but if there was some other character there, it might be interpreted as part of the variable name.

点在这里为您完成工作,但如果那里还有其他字符,它可能会被解释为变量名称的一部分。

回答by ghostdog74

dont' leave spaces between "="

不要在“=”之间留空格

J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"