Here's the script.


set -e
echo ""

declare -i value=00
unset optics_status

optics_status=$(curl -I xx.xx.xx.xx:4512 | grep 'X-Trace' | awk '{print $NF}' | rev |cut -c 1-3 | rev)

if [ $optics_status != $value ]; then
    echo "result is different means I am in if loop"
    echo "stats is $optics_status"
    echo "result is equal means I am in else loop"
    echo "stats is $optics_status"

xx.xx.xx.xx is a server IP and if I run the curl command separately this is the output I get.

user@machine-lap:~/Documents$ curl -I xx.xx.xx.xx:4512 | grep 'X-Trace' | awk '{print $NF}' | rev |cut -c 1-3 | rev
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  8359    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0

As you see I've set value to 00 and the output of optics_status is also 00. So according to the if loop, it should echo i am in else loop but it just echos the i am in if loop and I can't understand why.

To put it in simpler words, its echoing the if statement instead of else.

  • 2
    An integer variable in bash does not retain zero padding. The string "$value" would be "0". – Kusalananda May 7 '18 at 10:13
  • And be aware that if you use bash arithmetic on non-zero numbers with a leading zero digit it will treat them as octal. Generally it's better to apply padding at the last possible moment - in the output statement, for example. – roaima May 7 '18 at 11:45

If you want your value variable to be 00, don't declare it as an integer in bash. Declaring it as an integer with declare -i will prompt the shell to evaluate it as an integer, and the integer value of 00 is zero.

Observe, the following code will output not same:

declare -i value='00'

if [ "$optics_status" = "$value" ]; then
   echo 'same'
   echo 'not same'

Changing declare -i value='00' to value='00' will make it output same.

Also note the quoting of the variable expansions above.

You could of course also consider promoting both variables' values to integers by comparing them using either

if [ "$optics_status" -ne "$value" ]; then


if (( optics_status != value )); then

but that assumes that you know that $optics_value is always going to be a string that may be converted to an integer (and a decimal integer rather than an octal integer too).

  • @Vlastimil I look at questions that interest me. We don't really do pure code reviews here though. I believe there's a separate SE site for that at codereview.stackexchange.com (I'm not on it, maybe I should be) – Kusalananda May 7 '18 at 10:38

cut's ranges are inclusive, so you've pulled three characters out. One of those characters might be invisible, such as a space, tab or newline. That makes the string different from the value 00.

In general, try set -o xtrace to see what happens when running your script. And Use More Quotes™!

  • even if i remove the curl command completely and just set "optics_status=00", it still echos the if statement and not else. – Nishant May 7 '18 at 9:48
  • thanks for your input l0b0. As @Kusalananda suggested above, the issue was that I declared "value" as an integer and bash would consider it a "0" instead of "00". – Nishant May 7 '18 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.