Home > Net >  Error 'not found' when declaring variables correctly
Error 'not found' when declaring variables correctly

Time:04-30

I have a file called "s1" and it contains the following:

#!/bin/bash
var1=5
var2=4
if (( var1 > var2 ))
 then 
  echo "$var1 more than $var2"
fi

On executing the file I get the following:

s1: 4: var1: not found

In most cases, such an error occurs due to incorrect declaration of variables, for example:

var1 = 5

but I'm declaring the variable without any extra spaces.
While executing this script, I noticed that I have created two files with the names 4 and var2 respectively Why am I getting such an error?

CodePudding user response:

You're running the script with sh, not bash. For example you might be doing sh s1. You should be doing ./s1 so that the system will use the shebang (#!/bin/bash) to determine which interpreter to run the script with.

I'm not sure how sh interprets double-parens (( )), but it's certainly not the same as Bash, so it's trying to run a command called var1 and redirect the output to a file called var2.

As for why you have a file called 4, you probably tried adding a dollar sign $ at some point.

  •  Tags:  
  • bash
  • Related