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.

jww
  • 97,681
  • 90
  • 411
  • 885
CJ.
  • 593
  • 1
  • 4
  • 4
  • 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. – BullShark Nov 27 '12 at 07:46

3 Answers3

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

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 21
    Oh man, that is so depressing. That was my problem. Thanks! – CJ. Nov 11 '09 at 11:35
  • 1
    Thanks Andrew Hare. Just had the same depressing problem! – Matt Bannert Aug 17 '10 at 13:16
  • 4
    I really don't understand why bash doesn't accept space :( – Sungguk Lim Dec 16 '11 at 06:15
  • 1
    I was about to pray God! Tears... – ImMathan Jul 14 '17 at 07:54
  • 1
    "notice I have removed the spaces from either side of the = " <- This is the simplest yet most useful solution I have seen recently. – MÇT Jun 19 '18 at 06:32
  • I have something similar but not working still. WTF? – TheRealChx101 Jul 26 '19 at 19:44
  • Thank you for this simple yet, obvious solution. Just need to learn syntax of bash better. – atom88 Apr 30 '20 at 15:54
  • I don't know how this is hard to understand. If you put spaces, you're telling bash to do it's JOB and run a command. Variables are the exception to the rule, not commands. "But why can't bash just accept spaces after variable name." If you're still somehow asking this, then imagine this: `echo = hi` - this should print `= hi` to the terminal, but if we went by the suggested logic, this would instead set a variable named `echo` to `hi`. – Matthew Dec 25 '20 at 06:22
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.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
7

dont' leave spaces between "="

J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"
ghostdog74
  • 327,991
  • 56
  • 259
  • 343