# checking a variable is within range

**URL:** https://community.unix.com/t/checking-a-variable-is-within-range/164310
**Category:** Shell Programming and Scripting
**Created:** [August 16, 2006, 11:40pm UTC](https://community.unix.com/t/checking-a-variable-is-within-range/164310 "2006-08-16T23:40:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Poison\_Ivy](https://community.unix.com/letter_avatar/poison_ivy/32/5_5575768a8748004e209b776fc1b2916d.png) [@Poison\_Ivy](https://community.unix.com/u/Poison_Ivy)
#### Post date: [August 16, 2006, 11:40pm UTC](https://community.unix.com/t/checking-a-variable-is-within-range/164310/1 "2006-08-16T23:40:16Z")

</div>

how can i check that a variable is between 0-100, like if i ask a user to input a number between 1-100 and i want to excute commands WHILE that number is between that range or else i will keep asking the user to make another input

here's what i got

```nohighlight
#!/bin/bash
echo "Guess my secret number (0-100): "
number=$((RANDOM%100+0))
echo "$number"
read GUESS

```

I've tried all sorts of things but I keep getting errors, i tried:

```nohighlight
if echo $GUESS | grep "^[0-9]*$">aux
then
  echo "good"
else
  echo "bad"
fi
rm aux

```

and i've tried

```nohighlight
if [ -n "$( print - "$GUESS" |\
            sed 's/^[+-]//;s/[0-9]//g;s/\.//' \
          )" ] ; then
     echo "good"
else
     echo "bad"
fi

```

i'm always getting errors like command not found and unexpected end of file, can anyone pls help me?

---

<div class="post-metadata">

### Author: ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)
#### Post date: [August 17, 2006, 12:43am UTC](https://community.unix.com/t/checking-a-variable-is-within-range/164310/2 "2006-08-17T00:43:41Z")

</div>

This will validate a number between 0 and 100 both inclusive.

```nohighlight
[/tmp]$ cat try.sh
#! /bin/sh

echo "Guess my secret number (0-100): "
read GUESS

if [[$GUESS -ge 0 && $GUESS -le 100]] ; then 
  echo "good"
else
  echo "bad"
fi

```

---

<div class="post-metadata">

### Author: ![Poison\_Ivy](https://community.unix.com/letter_avatar/poison_ivy/32/5_5575768a8748004e209b776fc1b2916d.png) [@Poison\_Ivy](https://community.unix.com/u/Poison_Ivy)
#### Post date: [August 17, 2006, 5:33am UTC](https://community.unix.com/t/checking-a-variable-is-within-range/164310/3 "2006-08-17T05:33:59Z")

</div>

Thanks 🙂
