`integer expression expected` [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite













This question already has an answer here:



  • How to use variables inside single quotes

    2 answers



I encountered integer expression expected error in the following code:



 #! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi

if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi


Run it and get error report



 $ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.


I checked multiple times but fail to locate the bug,

which seem to line-to-line comform to the book



What's the problem with my code?







share|improve this question












marked as duplicate by Kusalananda, Isaac, Jeff Schaller, Community♦ Apr 2 at 23:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
    – Gilles Quenot
    Apr 2 at 15:15














up vote
0
down vote

favorite













This question already has an answer here:



  • How to use variables inside single quotes

    2 answers



I encountered integer expression expected error in the following code:



 #! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi

if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi


Run it and get error report



 $ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.


I checked multiple times but fail to locate the bug,

which seem to line-to-line comform to the book



What's the problem with my code?







share|improve this question












marked as duplicate by Kusalananda, Isaac, Jeff Schaller, Community♦ Apr 2 at 23:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
    – Gilles Quenot
    Apr 2 at 15:15












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • How to use variables inside single quotes

    2 answers



I encountered integer expression expected error in the following code:



 #! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi

if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi


Run it and get error report



 $ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.


I checked multiple times but fail to locate the bug,

which seem to line-to-line comform to the book



What's the problem with my code?







share|improve this question













This question already has an answer here:



  • How to use variables inside single quotes

    2 answers



I encountered integer expression expected error in the following code:



 #! /bin/bash
# test integer: evaluate the value of integer
int=-5
if [ -z "$int" ]; then
echo 'int is empty.' >&2
fi

if [ "$int" -eq 0 ]; then
echo "int is zero"
else
if [ '$int' -lt 0 ]; then
echo "int is negative."
else
echo 'int is positive.'
fi
if [ $((int % 2)) -eq 0 ]; then
echo 'int is even.'
else
echo 'int is odd.'
fi
fi


Run it and get error report



 $ bash test_integer.sh
test_integer.sh: line 14: [: $int: integer expression expected
int is positive.
int is odd.


I checked multiple times but fail to locate the bug,

which seem to line-to-line comform to the book



What's the problem with my code?





This question already has an answer here:



  • How to use variables inside single quotes

    2 answers









share|improve this question











share|improve this question




share|improve this question










asked Apr 2 at 15:09









JawSaw

29410




29410




marked as duplicate by Kusalananda, Isaac, Jeff Schaller, Community♦ Apr 2 at 23:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Kusalananda, Isaac, Jeff Schaller, Community♦ Apr 2 at 23:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
    – Gilles Quenot
    Apr 2 at 15:15
















  • FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
    – Gilles Quenot
    Apr 2 at 15:15















FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
– Gilles Quenot
Apr 2 at 15:15




FAQ: mywiki.wooledge.org/BashFAQ | Guide: mywiki.wooledge.org/BashGuide | Ref: gnu.org/s/bash/manual | wiki.bash-hackers.org | mywiki.wooledge.org/Quotes | Check your script: shellcheck.net And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.
– Gilles Quenot
Apr 2 at 15:15










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










'$int' is a quoting error which you would have easily seen if you had run the script through



bash -vx schript.sh


There is no expansion within single quotes. You need:



if [ "$int" -lt 0 ]





share|improve this answer



























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    '$int' is a quoting error which you would have easily seen if you had run the script through



    bash -vx schript.sh


    There is no expansion within single quotes. You need:



    if [ "$int" -lt 0 ]





    share|improve this answer
























      up vote
      4
      down vote



      accepted










      '$int' is a quoting error which you would have easily seen if you had run the script through



      bash -vx schript.sh


      There is no expansion within single quotes. You need:



      if [ "$int" -lt 0 ]





      share|improve this answer






















        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        '$int' is a quoting error which you would have easily seen if you had run the script through



        bash -vx schript.sh


        There is no expansion within single quotes. You need:



        if [ "$int" -lt 0 ]





        share|improve this answer












        '$int' is a quoting error which you would have easily seen if you had run the script through



        bash -vx schript.sh


        There is no expansion within single quotes. You need:



        if [ "$int" -lt 0 ]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 2 at 15:15









        Hauke Laging

        53.2k1282130




        53.2k1282130












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Christian Cage

            How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?