# Comparing integers - without calling the shell

**URL:** <https://discuss.kakoune.com/t/comparing-integers-without-calling-the-shell/1635>\
**Category:** Recipes & Guides\
**Created:** [March 18, 2021, 8:28pm UTC](https://discuss.kakoune.com/t/comparing-integers-without-calling-the-shell/1635 "2021-03-18T20:28:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ftonneau](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ftonneau/32/295_2.png) [@ftonneau](https://discuss.kakoune.com/u/ftonneau)\
**Post date:** [March 18, 2021, 8:28pm UTC](https://discuss.kakoune.com/t/comparing-integers-without-calling-the-shell/1635/1 "2021-03-18T20:28:47Z")

</div>

In the infamous series:

> [@Branching on a boolean option -- without calling the shell](https://discuss.kakoune.com/t/branching-on-a-boolean-option-without-calling-the-shell/929):
>
> In Kakoune scripts, branching on the value of a boolean option, myvar, may be done by calling the shell inside a try ... catch construction: try %{ evaluate-commands %sh{ if ["$kak\_opt\_myvar" = false]; then printf %s\\n fail fi } commands\_1 } \ catch %{ commands\_2 } which looks bloated and contorted to me. (One could use a wrapper to make the code look simpler, but then the wrapper needs to be defined elsewhere.) Recently I have been using a trick…

here is another dirty hack to compare non-negative integers in Kakoune, without calling the shell. The hack depends on subtracting an integer from another integer with the `set -add` command, as well as the behavior of `echo`: when followed by a non-negative integer, echo does its job, but when followed by a negative integer (e.g., `-3`), `echo` interprets the leading minus sign as the start of an option and protests (‘unknown option’).

Here is the code:

```
declare-option -hidden int int_running_sum

define-command \
-params 2 \
equal-to %{
    less-or-equal %arg(1) %arg(2)
    greater-or-equal %arg(1) %arg(2)
}

define-command \
-params 2 \
different-from %{
    try %{
        less-than %arg(1) %arg(2)
    } \
    catch %{
        greater-than %arg(1) %arg(2)
    }
}

define-command \
-params 2 \
less-than %{
    set window int_running_sum %arg(2)
    set -add window int_running_sum "-%arg(1)"
    set -add window int_running_sum -1
    evaluate-commands -draft %{
        echo %opt(int_running_sum)
    }
}

define-command \
-params 2 \
less-or-equal %{
    set window int_running_sum %arg(2)
    set -add window int_running_sum "-%arg(1)"
    evaluate-commands -draft %{
        echo %opt(int_running_sum)
    }
}

define-command \
-params 2 \
greater-than %{
    set window int_running_sum %arg(1)
    set -add window int_running_sum "-%arg(2)"
    set -add window int_running_sum -1
    evaluate-commands %{
        echo %opt(int_running_sum)
    }
}

define-command \
-params 2 \
greater-or-equal %{
    set window int_running_sum %arg(1)
    set -add window int_running_sum "-%arg(2)"
    evaluate-commands %{
        echo %opt(int_running_sum)
    }
}

```

Enjoy … or not!

(I do have one use case for this sort of trick.)

---

<div class="post-metadata">

**Author:** ![danr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/danr/32/700_2.png) [@danr](https://discuss.kakoune.com/u/danr)\
**Post date:** [March 19, 2021, 10:19am UTC](https://discuss.kakoune.com/t/comparing-integers-without-calling-the-shell/1635/2 "2021-03-19T10:19:02Z")

</div>

> echo does its job, but when followed by a negative integer (e.g., -3), echo interprets the leading minus sign as the start of an option and protests (‘unknown option’)

Gwahahaha, that’s just… great.
