Shell脚本变量 - 找不到命令[重复]

时间:2022-01-06 11:09:58

This question already has an answer here:

这个问题在这里已有答案:

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。

3 个解决方案

#1


94  

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不喜欢空格 - 也最好是引用每个值(但这不是必需的)。

#2


11  

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.

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

#3


6  

dont' leave spaces between "="

不要在“=”之间留空格

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

#1


94  

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不喜欢空格 - 也最好是引用每个值(但这不是必需的)。

#2


11  

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.

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

#3


6  

dont' leave spaces between "="

不要在“=”之间留空格

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