Bash variable assignment and command not found

BashShellVariablesCygwin

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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.

Solution 3 - Bash

dont' leave spaces between "="

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCJ.View Question on Stackoverflow
Solution 1 - BashAndrew HareView Answer on Stackoverflow
Solution 2 - BashDennis WilliamsonView Answer on Stackoverflow
Solution 3 - Bashghostdog74View Answer on Stackoverflow