Bash Command Not Found When Setting a Variable

Command not found error in Bash variable assignment

You cannot have spaces around the = sign.

When you write:

STR = "foo"

bash tries to run a command named STR with 2 arguments (the strings = and foo)

When you write:

STR =foo

bash tries to run a command named STR with 1 argument (the string =foo)

When you write:

STR= foo

bash tries to run the command foo with STR set to the empty string in its environment.

I'm not sure if this helps to clarify or if it is mere obfuscation, but note that:

  1. the first command is exactly equivalent to: STR "=" "foo",
  2. the second is the same as STR "=foo",
  3. and the last is equivalent to STR="" foo.

The relevant section of the sh language spec, section 2.9.1 states:

A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.

In that context, a word is the command that bash is going to run. Any string containing = (in any position other than at the beginning of the string) which is not a redirection and in which the portion of the string before the = is a valid variable name is a variable assignment, while any string that is not a redirection or a variable assignment is a command. In STR = "foo", STR is not a variable assignment.

Error command not found when setting value to variable

You need to use the eval function, like

#!/bin/bash
foo=0
bar=foo;
eval "${bar}=1"
echo $foo;

The ${bar}=1 will first go through the substitution process so it becomes foo=1, and then the eval will evaluate that in context of your shell

Bash variable assignment and command not found

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 command not found when setting a variable

You define variables with var=string or var=$(command).

So you have to remove the leading $ and any other signs around =:

tag_name="proddeploy-$(date +"%Y%m%d_%H%M")"

deploy_date=$(date +"%Y%m%d_%H%M")
^^ ^

From Command substitution:

The second form `COMMAND` is more or less obsolete for Bash, since it
has some trouble with nesting ("inner" backticks need to be escaped)
and escaping characters. Use $(COMMAND), it's also POSIX!

Also, $() allows you to nest, which may be handy.

Getting command not found error while comparing two strings in Bash

This is problem:

if [[$variable == $blanko]];

Spaces are required inside square brackets, use it like this:

[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"

Bash: command not found on simple variable assignment

Putting a variable on a line by itself will execute the command stored in the variable. That an assignment is being performed at the same time is incidental.

In short, don't do that.

echo ${something:="false"}
echo ${something_else:="blahblah"}
echo ${name:="file.ext"}

How can I fix a command not found error given for a variable assignment in my Bash script?

A sample execution of your script returns following (error) output

sh ./test.sh 
Enter assignment mark (0 to 40):20
Enter Test1 mark (0 to 15):3
Enter Test2 mark (1 to 15):10
Enter Final exam mark (1 to 30):20
./test.sh: line 8: grades: command not found
./test.sh: line 9: [: -ge: unary operator expected
./test.sh: line 12: [: -ge: unary operator expected
./test.sh: line 15: [: -ge: unary operator expected
./test.sh: line 18: [: -ge: unary operator expected
./test.sh: line 21: [: -ge: unary operator expected
./test.sh: line 24: [: -ge: unary operator expected
./test.sh: line 27: [: -ge: unary operator expected
./test.sh: line 30: [: -ge: unary operator expected
./test.sh: line 33: [: -ge: unary operator expected
./test.sh: line 36: [: -ge: unary operator expected
./test.sh: line 39: [: -ge: unary operator expected
./test.sh: line 42: [: -ge: unary operator expected
./test.sh: line 45: [: -ge: unary operator expected
Wrong input

Where the error ./test.sh: line 8: grades: command not found implies that there must be something wrong with your grades assignment (and possibly the calculation).

grades = $(($assignment + $testo + $testa + $finalexam))

If you want to use this ((result=a+b)) bash syntax for your integer arithmetic and variable assignment then you need to change it to

((grades = assignment + testo + testa + finalexam))

Then the sample output will be as expected

sh ./test.sh 
Enter assignment mark (0 to 40):20
Enter Test1 mark (0 to 15):3
Enter Test2 mark (1 to 15):10
Enter Final exam mark (1 to 30):20
Your final grade is D

Addendum

As pointed out @BenjaminW. in the comment Another thing to fix (and maybe point out) is the quoting of variables in [...]; in your if-statement(s)

if [ $grades -ge 0 ] && [ $grades -lt 49 ]

you reference to your $grades variable without enclosing it in double quotes. That's usually not a good idea (and a source of errors as well as confusion) because referring to a variable without double quotes allows for reinterpretation (of therein contained special characters) and word splitting (if there are space characters) rather than simply only replacing the variable with its value – which is what you actually want. Thus, your if-statement(s) should use "$grades" instead as in

if [ "$grades" -ge 0 ] && [ "$grades" -lt 49 ]

But there's more. Because you're using Bash prefer using [[ ]] instead of [ ]; and then you also don't need to expand your variable with $ for your arithmetic comparisons. With this your if-statement becomes

if [[ grades -ge 0 && grades -lt 49 ]] 

Now given that you want to do arithmetic comparisons you should actually use (( )), and then your if-statement looks like this

if (( grades >= 0 && grades < 49 ))

The revised Bash script is then this

#!/bin/bash

read -p "Enter assignment mark (0 to 40):" assignment
read -p "Enter Test1 mark (0 to 15):" testo
read -p "Enter Test2 mark (1 to 15):" testa
read -p "Enter Final exam mark (1 to 30):" finalexam

((grades = assignment + testo + testa + finalexam))

if (( grades >= 0 && grades < 49 ))
then
echo "Your final grade is F"
elif (( grades >= 50 && grades < 52 ))
then
echo "Your final grade is D-"
elif (( grades >= 53 && grades < 56 ))
then
echo "Your final grade is D"
elif (( grades >= 57 && grades < 59 ))
then
echo "Your final grade is D+"
elif (( grades >= 60 && grades < 62 ))
then
echo "Your final grade is C-"
elif (( grades >= 63 && grades < 66 ))
then
echo "Your final grade is C"
elif (( grades >= 67 && grades < 69 ))
then
echo "Your final grade is C+"
elif (( grades >= 70 && grades < 72 ))
then
echo "Your final grade is B-"
elif (( grades >= 73 && grades < 76 ))
then
echo "Your final grade is B"
elif (( grades >= 77 && grades < 79 ))
then
echo "Your final grade is B+"
elif (( grades >= 80 && grades < 84 ))
then
echo "Your final grade is A-"
elif (( grades >= 85 && grades < 89 ))
then
echo "Your final grade is A"
elif (( grades >= 90 && grades < 100 ))
then
echo "Your final grade is A+"
else
echo "Wrong input"
fi

These are further helpful existing Q/As to refer to:

  • BASH: Basic if then and variable assignment
  • When to wrap quotes around a shell variable?
  • Comparing integers: arithmetic expression or conditional expression

Script gives if: command not found

I can solve this by using do eval "$HI". Of course, the problem is that if I use double quotes, then expansions are done at the first run. I don't want it to be expanded, but evaluated at every run.

EDIT: I got around this by using \$, or even better, using single quotes (this did mean I had to escape all the single quotes though).

EDIT 2: After consideration, I just decided to use functions without eval -- dealing with escaping was not worth it.



Related Topics



Leave a reply



Submit