59

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.

#!/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.

1
  • 2
    Don't use all CAPS in your variable names when creating variables. By convention, environment variables (PATH, EDITOR, SHELL, ...) and internal shell variables (BASH_VERSION, RANDOM, ...) are fully capitalized. All other variable names should be lowercase. Since variable names are case-sensitive, this convention avoids accidentally overriding environmental and internal variables. Commented Nov 27, 2012 at 7:46

3 Answers 3

133

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

Sign up to request clarification or add additional context in comments.

8 Comments

Oh man, that is so depressing. That was my problem. Thanks!
Thanks Andrew Hare. Just had the same depressing problem!
I really don't understand why bash doesn't accept space :(
I was about to pray God! Tears...
"notice I have removed the spaces from either side of the = " <- This is the simplest yet most useful solution I have seen recently.
|
12

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.

2 Comments

Using {} to delimit variables in the middle of text is a best practice for shell programming.
This was my problem! The first solution didn't work for me, but this did.
7

dont' leave spaces between "="

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.