Bash variable assignment and command not found [duplicate] - bash

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
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.

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

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.

dont' leave spaces between "="
J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"

Related

Bash: Storing a command in a variable CMD and then running it with $CMD fails [duplicate]

This question already has an answer here:
How can I create a bash environment variable that prefixes an environment variable before a command?
(1 answer)
Closed 1 year ago.
On Bash, I first define a variable CMD for a command line bash instruction, then I run it. An error occurs. Where does it go wrong?
$ CMD="VERBOSE=1 ./myscript"
$ $CMD
bash: VERBOSE=1: command not found
Don't store commands in variables. Variables aren't smart enough to hold commands. They will let you down time and time again like your roommate that never washes the dishes.
Use a function. They're the right tool for the job. You can use them like they were regular executables; they can take arguments; there are no quoting/backslash/whitespace issues.
$ cmd() {
> VERBOSE=1 ./myscript
> }
$ cmd
Functions are where it's at.
See also:
How can I create a bash environment variable that prefixes an environment variable before a command?

Bash - length of command line argument $0 [duplicate]

This question already has answers here:
How to create shell variable with dashes?
(2 answers)
Closed 1 year ago.
I want to find the length of $0
If I use the following bash code
leading-space=${#0}
I get the following result:
./Install: line 25: leading-space=9: command not found
$0 has the value ¨./Install¨
The length of the string appears to be correct, but then bash gets confused. I am running bash 5.0.17 on Ubuntu 20.10. Can anyone see what I am doing wrong?
Identifiers in bash may not contain -, therefore the whole assignment is interpreted as a command (like cd, or grep) but there is no command with the name leading-space=9 on your system, resulting in the error.
Use the following:
leading_space=${#0}

Command substitution with variables gives command not found? [duplicate]

This question already has answers here:
Getting "command not found" error in bash script
(6 answers)
Closed 3 years ago.
I'm having a BASH script:
#!/usr/bin/env bash
PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
YAML=$(envsubst < ${PATH}/test.yml)
echo "${YAML}"
It results:
./test.sh: line 5: envsubst: command not found
I see that having a variable inside command substitution causes such error.
If I'm not using any:
YAML=$(envsubst < ./test.yml)
Then I'm having an expected successful script execution.
I've tried different syntax using quotes all over the place, but nothing helped.
How do I successfully use variable inside command substitution?
Bash version:
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
UPDATE: A duplicate question. Found an answer here:
https://stackoverflow.com/a/5642584/5935309
The problem is that you are changing PATH, which is used by Bash internally to define where to look for programs (and in which order).
You changed PATH to only contain the current working dir, and that's not where envsubst is located.
The solution is to use something different than PATH, like FILE_PATH.

Running a command line through shell script is not working [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I have an application in Unix. I use the below command to connect to it:
./application -a "connect"
I want to do the same through the shell script, for which i assigned the command line to a variable like:
newcommand = './application -a "connect"'
$newcommand
But this is not working.
However the first part of the code is working. i.e.,:
newcommand = "./application"
$newcommand
Can anyone point out what i am missing.
Believe it or not, this:
newcommand = "./application"
...has the shell run the command, newcommand with the arguments, =, and ./application.
In shell simple assignments cannot have any unprotected whitespace or they'll be interpreted as a command.
Consider:
newcommand=./application
$newcommand
...notice that there's no space around the = sign in the assignment.

Assigning value not working in a shell [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
OS: Ubuntu 14.04
I created a test file called test and in it I have:
#!/bin/bash
NEW_VALUE = "/home/"
echo $NEW_VALUE
chmod +x test
./test
Produces the following:
./test: line 2: NEW_VALUE: command not found
Any ideas why this is not working?
In Bash you can't have any space around the = sign when assigning a variable.
Any space will end the assignment, even after the =, e.g.:
test_var=this is bad
#=> is: command not found
#CharlesDuffy's comment explaining why this happens
Check this link for more information on variable assignment in bash: http://wiki.bash-hackers.org/scripting/newbie_traps#setting_variables
Remove the white space while assigning then it'll work fine. just like:
#!/bin/bash
NEW_VALUE="/home/"
echo $NEW_VALUE

Resources