Ошибка integer expression expected

I’m a newbie to shell scripts so I have a question. What Im doing wrong in this code?

#!/bin/bash
echo " Write in your age: "
read age
if [ "$age" -le "7"] -o [ "$age" -ge " 65" ]
then
echo " You can walk in for free "
elif [ "$age" -gt "7"] -a [ "$age" -lt "65"]
then
echo " You have to pay for ticket "
fi

When I’m trying to open this script it asks me for my age and then it says

./bilet.sh: line 6: [: 7]: integer expression expected
./bilet.sh: line 9: [: missing `]'

I don’t have any idea what I’m doing wrong. If someone could tell me how to fix it I would be thankful, sorry for my poor English I hope you guys can understand me.

chepner's user avatar

chepner

493k71 gold badges520 silver badges676 bronze badges

asked Oct 21, 2013 at 21:36

user2904832's user avatar

3

You can use this syntax:

#!/bin/bash

echo " Write in your age: "
read age

if [[ "$age" -le 7 || "$age" -ge 65 ]] ; then
    echo " You can walk in for free "
elif [[ "$age" -gt 7 && "$age" -lt 65 ]] ; then
    echo " You have to pay for ticket "
fi

answered Oct 21, 2013 at 21:43

kamituel's user avatar

kamituelkamituel

34.4k6 gold badges81 silver badges98 bronze badges

2

If you are using -o (or -a), it needs to be inside the brackets of the test command:

if [ "$age" -le "7" -o "$age" -ge " 65" ]

However, their use is deprecated, and you should use separate test commands joined by || (or &&) instead:

if [ "$age" -le "7" ] || [ "$age" -ge " 65" ]

Make sure the closing brackets are preceded with whitespace, as they are technically arguments to [, not simply syntax.

In bash and some other shells, you can use the superior [[ expression as shown in kamituel’s answer. The above will work in any POSIX-compliant shell.

Community's user avatar

answered Oct 21, 2013 at 21:45

chepner's user avatar

chepnerchepner

493k71 gold badges520 silver badges676 bronze badges

1

This error can also happen if the variable you are comparing has hidden characters that are not numbers/digits.

For example, if you are retrieving an integer from a third-party script, you must ensure that the returned string does not contain hidden characters, like "n" or "r".

For example:

#!/bin/bash

# Simulate an invalid number string returned
# from a script, which is "1234n"
a='1234
'

if [ "$a" -gt 1233 ] ; then
    echo "number is bigger"
else
    echo "number is smaller"
fi

This will result in a script error : integer expression expected because $a contains a non-digit newline character "n". You have to remove this character using the instructions here: How to remove carriage return from a string in Bash

So use something like this:

#!/bin/bash

# Simulate an invalid number string returned
# from a script, which is "1234n"
a='1234
'

# Remove all new line, carriage return, tab characters
# from the string, to allow integer comparison
a="${a//[$'trn ']}"

if [ "$a" -gt 1233 ] ; then
    echo "number is bigger"
else
    echo "number is smaller"
fi

You can also use set -xv to debug your bash script and reveal these hidden characters. See https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-error-integer-expression-expected-934465/

answered Feb 9, 2018 at 11:56

Mr-IDE's user avatar

Mr-IDEMr-IDE

6,8991 gold badge53 silver badges59 bronze badges

1

./bilet.sh: line 6: [: 7]: integer expression expected

Be careful with " "

./bilet.sh: line 9: [: missing `]'

This is because you need to have space between brackets like:

if [ "$age" -le 7 ] -o [ "$age" -ge 65 ]

look: added space, and no " "

Dan Lowe's user avatar

Dan Lowe

50.8k20 gold badges119 silver badges111 bronze badges

answered May 11, 2017 at 14:28

circassia_ai's user avatar

Try this:

If [ $a -lt 4 ] || [ $a -gt 64 ] ; then n
     Something something n
elif [ $a -gt 4 ] || [ $a -lt 64 ] ; then n
     Something something n
else n
    Yes it works for me :) n

Juan Serrats's user avatar

Juan Serrats

1,3585 gold badges24 silver badges30 bronze badges

answered Jul 10, 2017 at 8:24

Harry1992's user avatar

Harry1992Harry1992

4531 gold badge5 silver badges12 bronze badges

1

If you are just comparing numbers, I think there’s no need to change syntax, just correct those lines, lines 6 and 9 brackets.

Line 6 before: if [ «$age» -le «7»] -o [ «$age» -ge » 65″ ]

After: if [ "$age" -le "7" -o "$age" -ge "65" ]

Line 9 before: elif [ «$age» -gt «7»] -a [ «$age» -lt «65»]

After: elif [ "$age" -gt "7" -a "$age" -lt "65" ]

answered Apr 27, 2020 at 16:37

markfree's user avatar

Integer expression expected error can occur due to using the wrong bash syntax around integer type of variables, not having perfect integers in your code, etc. However, it’s possible that the problem is not obvious enough to be observed at first glance.integer expression expected

Thus, this post has put together some relevant examples that’ll ease your journey of finding the problem in your code and help you solve it with minimum effort. So, look nowhere else and keep reading this article to see how it makes your life easy in the first read.

Contents

  • Which Problems Can Result in the Integer Expression Expected Error?
    • – Invalid Bash Syntax
    • – A Variable Containing Other Characters Except for Numbers
    • – You Are Comparing Float Numbers Without Using BC
  • Excellent Methods To Sort Out the Integer Expression Expected Error
    • – Correct the Syntax Errors
    • – Filter the Targeted Variables
    • – Get Started With the Basic Calculator
  • Conclusion

Which Problems Can Result in the Integer Expression Expected Error?

The integer expression expected error can be the result of using invalid bash syntax, specifically around integer values, or having variables that are expected to have only numbers but don’t fulfill the value expectation. Plus, performing operations on floats without using bc can be problematic.

– Invalid Bash Syntax

Using an invalid bash syntax, such as missing brackets, putting extra double quotes, etc., while working with integers can lead to the expr integer expression expected error. So, even if you provide the correct integer values, syntax mistakes can generate the said error.

Imagine that you are creating a bash script whose output will be based on the result of the two conditions wrapped inside the if statement. The conditions deal with comparing two integers, one is stored in a variable, and the other is an integer literal.

Here, you get confused while coding for the if statement and end up adding inappropriate square brackets. Consequently, you’ll get the integer expression expected in if condition error on your screen instead of your desired output.

You can have a look at the bash script that aligns with the stated scenario below.

#!/bin/bash

echo “Please enter your grade:”

read grade

if [ “$grade” -le 3 || “$grade” -ge 8 ] ; then

echo “Your classroom is on the second floor”

elif [ “$grade” -gt 3 ] && [ “$grade” -lt 8 ] ; then

echo “Your classroom is on the first floor”

fi

Similarly, if you don’t precede the variable like “num1” containing an integer with a “$” dollar sign, it will be considered a string containing “num1” and comparing it against an integer will cause the active integer expression expected error.

Here is what the above example talked about.

#!/bin/bash

echo “Enter Your Lucky Number:”

read num1

if [“num1” -le 10 ]; then

echo “The given number is less than 10. “

Furthermore, if you go wrong with the while statement syntax, the integer expression expected bash while error will occur on your system. It can be any mistake, like placing the brackets incorrectly or not preceding the variable name with a “$” sign, as shown in the previous integer expression examples.

– A Variable Containing Other Characters Except for Numbers

If you are using a variable containing an integer value instead of an integer literal, then it might be possible that the variable doesn’t contain only numbers, which can generate the error. Note that you can’t treat a variable containing a combination of numbers and other characters as an integer.Integer Expression Expected Causes

For example, you have a “quantity” variable in your bash script with an invalid integer value like “60” where the number 60 has a space at its end. In this case, you won’t be able to use the quantity variable where an integer is required. If you do, the ksh integer expression expected error will show up.

The relevant code block has been provided below for your clarity.

#!/bin/bash

quanitity=’60 ’

if [“$quantity” -gt 100 ] ; then

echo “There are more than hundred pens in the store. ”

else

echo “The pens are soon going to be out of stock. ”

fi

– You Are Comparing Float Numbers Without Using BC

You might not know that you can’t perform operations on the float numbers, also known as numbers with decimal points in your bash script, without using the Basic Calculator (BC). Therefore, if you try to compute results or make comparisons using the float values, an error will occur.

Consider yourself in a situation where you are comparing two float values. As you are a new bash user, you don’t have an idea about using the BC. In such a scenario, you’ll get the integer expression expected float error in front of you.

The coding representation of the mentioned example can be found below.

#/bin/bash

str1= “Bash”

str2= “Bash”

val1= “1.2”

val2= “1.7”

if [[ $str1 == $str2 ]]

then

if [ $val1 > $val2 ]

then

echo “val1 is greater than val2”

else

echo “val1 is less than val2”

fi

else

echo “Values not found!”

fi

Excellent Methods To Sort Out the Integer Expression Expected Error

You can sort out the integer expression expected error by correcting the syntax mistakes in your script or applying regular expressions on the variables that are expected to store numbers only. Also, using the basic calculator to operate on float values is a good way to deal with the error.



– Correct the Syntax Errors

You must remove the syntax errors in your script to ensure that the ksh integer expression expected error goes away within seconds. The more you take care of the bash syntax, the farther you’ll be from the error discussed in this post.

Talking about the examples shared above, you’ll need to place the brackets following the if statement correctly to eliminate the error. Here you go with the correct version of the code.

#!/bin/bash

echo “Please enter your grade:”

read grade

if [ “$grade” -le 3 ] || [ “$grade” -ge 8 ] ; then

echo “Your classroom is on the second floor”

elif [ “$grade” -gt 3 ] && [ “$grade” -lt 8 ] ; then

echo “Your classroom is on the first floor”

fi

Moreover, you should always precede the variable names with the “$” symbol to use their values in your bash script.

– Filter the Targeted Variables

If you are sure about the correctness of the bash syntax, then you’ll need to filter the targeted variables to remove any special characters, such as newlines, tabs, etc. to get rid of the error. You can filter your required variable’s value by using regular expressions.Integer Expression Expected Fixes

To resolve the error occurring while using the quantity variable in an example earlier, you might need to create a regular expression like “${a//[$’ trn’]}.” It will remove any characters like tabs, carriage returns, newlines, and spaces standing at the end of the given variable’s value. Now, whenever you use your desired variable, the error won’t come shouting back.

See the working code block below.

#!/bin/bash

quanitity=’60 ’

quantity=“${quantity//[$’trn ‘]}.”

if [“$quantity” -gt 100 ] ; then

echo “There are more than hundred pens in the store. ”

else

echo “The pens are soon going to be out of stock. ”

fi

– Get Started With the Basic Calculator

If you want to work with float values and don’t want the above error to interrupt your script execution, you’ll need to use the basic calculator “bc” in your script. It will take you to extend the statement computing or comparing float numbers with “| bc” and wrap it in echo.

Look below to see how you can make the float values comparison example code run efficiently and eliminate the bash integer expression expected decimal error.

#/bin/bash

str1= “Bash”

str2= “Bash”

val1= “1.2”

val2= “1.7”

if [ $(echo “$str1 == $str2” | bc) ]

then

if [ $(echo “$val1 > $val2” | bc) ]

then

echo “val1 is greater than val2”

else

echo “val1 is less than val2”

fi

else

echo “Values not found!”

fi

Conclusion

Now, you have understood that the integer expression expected error is usually the result of the problematic integer variables or syntax around their location of usage. So, it demands a strong memory to remember the syntax and extra attention to the script details. Read a bit more if you don’t mind viewing an instant recap of this post.

  • It would be best to learn the bash syntax properly and double-check it to quickly find the coding mistakes, correct them, and resolve the error.
  • The regular expressions can help filter the unneeded characters from your integer variable and push away the error.
  • Extending your statement with bc is a must to let the error go away when it comes to the float values.

Lastly, always remember that the stated error screams about the integer expectations of your code, and you need to fulfill them in one way or the other to get rid of it.

  • Author
  • Recent Posts

Position is Everything

Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team

Position is Everything

By the look of things, your result variable has a . in it after the number making bash not recognise it as such. You can reproduce the error by simply doing:

[ 7. -gt 1 ]

If you add more of the script to your question, |I can suggest where this might be coming from.

Update

Looking at the full script, I would just replace the line:

result=$(echo "$used / $total * 100" |bc -l|cut -c -2)

With:

result=$(( 100 * used / total ))

Since used and total are integers and bash does integer arithmetic, though note the shifting of the multiplication be 100 to the beginning. Or if you want to ensure correct rounding (‘integer division’ in computing always effectively rounds down):

result=$( printf '%.0f' $(echo "$used / $total * 100" | bc -l) )

This will ensure that there are no trailing dots in result. The approach using cut is not a very good idea since it is only valid for result in the range 10-99. It will fail for a result from 0-9 (as in your case) and also numbers above 99.

Update 2

From @Stephane’s comment below, you are better to round down when comparing to thresholds. Considering this, there is another small error with the snippet in the question — notice the inconsistency between the comparisons used for the warn_level and the critical_level. The comparisons for warn_level are correct, but critical_level uses -le (lesser or equal) instead of -lt (just lesser). Consider when result is slightly larger than critical_level — it will be rounded down to critical_level and not trigger the critical warning even though it should (and would if a -lt comparison was used).

Perhaps not much of an issue, but here is the corrected code:

if [ "$result" -lt "$warn_level" ]; then
  echo "Memory OK. $result% used."
  exit 0;
elif [ "$result" -lt "$critical_level" ]; then
  echo "Memory WARNING. $result% used."
  exit 1;
else
  echo "Memory CRITICAL. $result% used."
  exit 2;
fi

The -ge tests are also redundant since these cases are implied on reaching the elif/else, so have been removed.

����� 31. ������ ���������������� ������

Turandot: Gli enigmi sono tre, la morte
una!

Caleph: No, no! Gli enigmi sono tre, una la
vita!

Puccini

������������� ����������������� ���� � ��������� �������� �
�������� ���� ����������.

case=value0       # ����� ������� ��������.
23skidoo=value1   # ���� �����.
# ����� ����������, ������������ � ����, ��������������� ��������� ���������.
# ���� ��� ���������� ���������� � ������� �������������: _23skidoo=value1, �� ��� �� ��������� �������.

# ������... ���� ��� ���������� ������� �� ������������� ������� �������������, �� ��� ������.
_=25
echo $_           # $_  -- ��� ���������� ����������.

xyz((!*=value2    # �������� ��������� ��������.

������������� ������, � ������ ����������������� ��������, �
������ ����������.

var-1=23
# ������ ����� ������ ����������� 'var_1'.

������������� ���������� ���� ��� ���������� � �������. ���
������ �������� ������� ��� ���������.

do_something ()
{
  echo "��� ������� ������ ���-������ ������� � "$1"."
}

do_something=do_something

do_something do_something

# ��� ��� ����� �������� ���������, �� ������� �� ���������.

������������� ������ ��������. � ������� �� ������
������ ����������������, Bash ������ ����������� �� ��������� �
��������.

var1 = 23   # ���������� �������: 'var1=23'.
# � ��������������� ������ Bash ����� ���������� "var1" ��� ��� �������
# � ����������� "=" � "23".

let c = $a - $b   # ���������� �������: 'let c=$a-$b' ��� 'let "c = $a - $b"'

if [ $a -le 5]    # ���������� �������: if [ $a -le 5 ]
# if [ "$a" -le 5 ]   ��� �����.
# [[ $a -le 5 ]] ���� �����.

��������� �������� ������������� � ���, ���
�������������������� ���������� �������� «����». ��������������������
���������� �������� «������» (null) ��������, � �� ����.

#!/bin/bash

echo "uninitialized_var = $uninitialized_var"
# uninitialized_var =

����� ������������ ������ ��������� ��������� =-eq. ���������, �������� = ������������ ��� ���������
��������� ����������, � -eq — ��� ��������� �����
�����.

if [ "$a" = 273 ]      # ��� �� ���������? $a -- ��� ����� ����� ��� ������?
if [ "$a" -eq 273 ]    # ���� $a -- ����� �����.

# ������, ������ ���� ������ ����� ���� �� ���������.
# ������...


a=273.0   # �� ����� �����.

if [ "$a" = 273 ]
then
  echo "�����."
else
  echo "�� �����."
fi    # �� �����.

# ���� ����� � ���  a=" 273"  �  a="0273".


# �������� �������� ��������� ��� ������������� "-eq" �� ���������� ����������.

if [ "$a" -eq 273.0 ]
then
  echo "a = $a'
fi  # ���������� �������� ����������� �� ������.
# test.sh: [: 273.0: integer expression expected

������ ��� ��������� ����� ����� � ��������� ��������.

#!/bin/bash
# bad-op.sh

number=1

while [ "$number" < 5 ]    # �������! ������ ����   while [ "number" -lt 5 ]
do
  echo -n "$number "
  let "number += 1"
done

# ���� �������� ���������� ��������� �� ������:
# bad-op.sh: 5: No such file or directory

������, � ��������� ��������, � �������������� ����������
������ ([ ]), ���������� ���������� ����� � ������� �������. ��.
������ 7-6, ������ 16-4 � ������ 9-6.

������ �������� �� � ��������� ��������� ������� ��-��
�������� ���� �������. ���� ������������ �� ������ ���������
������� �� ��������� ������, �� ��� ������� �� ������ ����
�������� � �� ��������. ���������� �������� �������� �������,
�������� ��� �������� ���������� ��� suid.

������������� ������� � �������� ��������� ���������������
(������� �� �� ��������) ����� ��������� � �����������
�����������.

command1 2> - | command2  # ������� �������� ��������� �� ������� ������� command1 ����� ��������...
#    ...�� ����� ��������.

command1 2>& - | command2  # ��� �� ������������.

������� S.C.

������������� �������������� ������������ Bash ������ 2 ��� ����, �����
�������� � ���������� ���������� ��������, ����������� ���
����������� Bash ������ 1.XX.

#!/bin/bash

minimum_version=2
# ��������� Chet Ramey ��������� ��������� Bash,
# ��� ����� ������������� ������� ������ ���������� ���������� ������ $minimum_version=2.XX.
E_BAD_VERSION=80

if [ "$BASH_VERSION" < "$minimum_version" ]
then
  echo "���� �������� ������ ����������� ��� ����������� Bash, ������ $minimum ��� ����."
  echo "������������ ������������� ����������."
  exit $E_BAD_VERSION
fi

...

������������� ������������� ������������ Bash ����� ���������
� ���������� ���������� �������� � Bourne shell (#!/bin/sh). ��� �������,
� Linux �������������, sh �������� ����������� bash, �� ��� �� ������ ����� ���
UNIX-������ ������.

��������, � ������� ������ ���������� ���� �� ����� � �����
MS-DOS (rn), ����� �����������
��������, ��������� ���������� #!/bin/bashrn
��������� ������������. ��������� ��� ������ ����� �������
��������� ������� r �� ��������.

#!/bin/bash

echo "������"

unix2dos $0    # �������� ��������� ������� �������� ������ � ������ DOS.
chmod 755 $0   # �������������� ���� �� ������.
               # ������� 'unix2dos' ������ ����� �� ������ �� ��������� �����.

./$0           # ������� ��������� ���� ������.
               # �� ��� �� ��������� ��-�� ����, ��� ������ ������ ����������
               # ���� �� ����� � ����� DOS.

echo "�����"

exit 0

��������, ������������ � #!/bin/sh, �� �����
�������� � ������ ������ ������������� � Bash. ��������� ��
������������� �������, �������� Bash, ����� ���������
������������ � �������������. ��������, ������� ������� �������
������� �� ���� �����������, ��������� � Bash, ������ ����������
������� #!/bin/bash.

�������� �� ����� �������������� ���������� ������������� �������� — ��������.
����� ��� � �������, ������� ����� ������������ ����� ��������,
�� �� ���������.

WHATEVER=/home/bozo
export WHATEVER
exit 0
bash$ echo $WHATEVER

bash$

������ ������� — ��� ������ � ��������� ������ ����������
$WHATEVER ��������� ��������������������.

������������� � ����������� ���������� � ���� �� �������, ���
� � ������������ �������� ����� �� ������ ����������
����������.

������ 31-1. ������� � �����������

#!/bin/bash
# ������� � �����������.

outer_variable=�������_����������
echo
echo "outer_variable = $outer_variable"
echo

(
# ������ � �����������

echo "������ ����������� outer_variable = $outer_variable"
inner_variable=����������_����������  # ����������������
echo "������ ����������� inner_variable = $inner_variable"
outer_variable=����������_����������  # ��� �������? ������� ������� ����������?
echo "������ ����������� outer_variable = $outer_variable"

# ����� �� �����������
)

echo
echo "�� ��������� ����������� inner_variable = $inner_variable"  # ������ �� ���������.
echo "�� ��������� ����������� outer_variable = $outer_variable"  # �������_����������.
echo

exit 0

�������� ������ �� echo �� ��������� ������� read ����� ������ �����������
����������. � ���� ��������, ������� read ��������� ���, ��� ����� �� ���
���� �������� � �����������. ������ ��� ����� ������������
������� set (��. ������ 11-14).

������ 31-2. �������� ������ �� ������� echo �������
read, �� ���������

#!/bin/bash
#  badread.sh:
#  ������� ������������� 'echo' � 'read'
#+ ��� ������ �������� � ����������.

a=aaa
b=bbb
c=ccc

echo "���� ��� ���" | read a b c
# ������� �������� �������� � ���������� a, b � c.

echo
echo "a = $a"  # a = aaa
echo "b = $b"  # b = bbb
echo "c = $c"  # c = ccc
# ������������ �� ���������.

# ------------------------------

# �������������� �������.

var=`echo "���� ��� ���"`
set -- $var
a=$1; b=$2; c=$3

echo "-------"
echo "a = $a"  # a = ����
echo "b = $b"  # b = ���
echo "c = $c"  # c = ���
# �� ���� ��� ��� � �������.

# ------------------------------

#  �������� ��������: � ����������� 'read', ��� ������� ��������, ���������� ������������� ���������.
#  �� ������ � �����������.

a=aaa          # ��� �������.
b=bbb
c=ccc

echo; echo
echo "���� ��� ���" | ( read a b c;
echo "������ �����������: "; echo "a = $a"; echo "b = $b"; echo "c = $c" )
# a = ����
# b = ���
# c = ���
echo "-------"
echo "�������: "
echo "a = $a"  # a = aaa
echo "b = $b"  # b = bbb
echo "c = $c"  # c = ccc
echo

exit 0

�������� ����, ��� ������������ �������, ������������
������������� � �������� ������, � ������������� ����� «suid». [1]

������������� ��������� � �������� CGI-���������� �����
��������� � ��������� ��������� ��-�� ���������� �������� �����
����������. ����� ����, ��� ����� ����� ���� �������� ����������
�� ��� ����������� ��������.

Bash �� ������ ��������� ������������ ������, ���������� ������� ���� (//).

�������� �� ����� Bash, ��������� ��� Linux ��� BSD ������,
����� ����������� ���������, ����� ��� ��� ��� ������ ����
�������� � ������������ ������ UNIX. ����� ��������, ��� �������,
���������� GNU-������ ������ � ������, ������� ����� ������
����������������, ������ �� ������� � UNIX. ��� ��������
����������� ��� ����� ������ ��������� ������, ��� tr.

Danger is near thee —

Beware, beware, beware, beware.

Many brave hearts are asleep in the deep.

So beware —

Beware.

A.J. Lamb and H.W.
Petrie

The Bash script is the most frequently used programming language for dealing with the Linux environment. It supports all the basics of datatypes of variables to perform operations such as comparing variables and generating results accordingly. However, the user might face the error of “integer expression expected” while writing any script, which stops the script’s working. The reason is typically caused when the wrong bash syntax is utilized such as using the wrong comparison operator.

This post will demonstrate the reasons and solutions for the error “integer expression expected error” in bash.

  • Incorrect Use of Comparison Operator
  • Solution: Utilize the Correct Comparison Operator

Reason: Incorrect Use of Comparison Operator

The error is invalid bash syntax, such as using the non-integer value instead of an integer while comparing them. In the following script, the two strings are compared to check whether they are equal or not using the “eq” operator, but the comparison operator is wrong here. As the “eq,” “lt,” and “gt” type of operator deals with integer values only so it will display the error that “integer expression expected”:

#!/bin/bash

string1="hello"
String2="hello"

if [ "$string1" -eq "$string2" ]
then
echo "Strings Are Equal"
fi

Save and run the script in the terminal:

The error occurred on the line “integer expression expected.”

Solution: Utilize the Correct Comparison Operator

To fix this error, use the correct Bash syntax and use the integer value where required instead of non-integer values. In our case, using the comparison operator for comparing the two strings will resolve the error.

Operators Scenarios to use
-eq, -lt, -gt, -ge, -le, -ne Use these operators when comparing operands, i.e., “integer.
=, != These operators are utilized when comparing operands are “string.” 

So, according to the above table, the “-eq” operator should be replaced with the “=” to compare the string in the script. Let’s apply this in our script and check the results:

#!/bin/bash

string1="hello"
String2="hello"

if [ "$string1"="$string2" ]
then
echo "Strings Are Equal"
fi

Once the script is modified, save and exit the file.

Run the script using the bash command in the terminal:

The error is resolved, and both strings are equal.

Conclusion

The “integer expression expected” error occurs if the wrong bash syntax is utilized, such as non-integer values instead of an integer. To fix this error, use the correct bash syntax for integer and non-integer values such as -eq, -lt, -gt, -ge, -le, and -ne operators require integer operands to compare. While “=” and “!=” operators require both strings operands to compare. 

This write-up has illuminated the reason and the solution for the error “integer expression expected” in the bash script.

  • Ошибка int008054 лицевой счет не найден
  • Ошибка int007087 для пу предоставляющих показания потребления кр должны указываться показания пу
  • Ошибка int002034 идентификатор объекта
  • Ошибка int object is not callable питон
  • Ошибка insurgency out of memory