Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have this code:

if [[ $1 = "-s" ]] && [[ $2 = 0-9 ]]
then
  echo "yes"
fi

0-9 does not work for me. What I really want is to type, for example, -x 3 (or any number).

share|improve this question

marked as duplicate by Braiam, Jakuje, dr01, Jeff Schaller, roaima Jan 10 at 16:16

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.

if [[ $1 = "-s" ]] && [[ $2 -ge 0 ]] && [[ $2 -le 9 ]]

-ge: greater-than-or-equal

-le: less-than-or-equal

share|improve this answer

This is possible, but you need to use a character class ([0-9]).For example:

if [[ "$1" = "-s" ]] && [[ "$2" = [0-9] ]]
then
    echo "yes"
fi

However, the above is only true of $2 consists of a single digit. If you want $2 to consist of one or more digits, use:

if [[ "$1" = "-s" ]] && [[ "$2" != *[!0-9]* && "$2" = [1-9]* ]]
then
    echo "yes"
fi

In newer bash versions, you can also use regular expressions:

if [[ "$1" = "-s" ]] && [[ "$2" =~ ^[0-9]+$ ]]
then
    echo "yes"
fi

The ^ matches the start of the string, and $ the end. The + means "0 or more". So, ^[0-9]*$ will only be true of the string consists of nothing but one or more digits from the beginning until its end.

share|improve this answer
2  
You don't need the regex match operator to test if a variable consists of a single digit: [[ "$1" = "-s" && "$2" = [0-9] ]]. You don't need the regex match operator to test if a variable consists solely of digits: [[ "$1" = "-s" && "$2" != *[!0-9]* && "$2" = [1-9]* ]] (here I also reject leading zeros and empty strings). – Gilles Jan 9 at 19:50
1  
Why are you breaking out the regexes for integer comparisons? – Blacklight Shining Jan 9 at 23:32
    
@BlacklightShining for no good reason. Simply because that was the first approach that occurred to me and because I like regexes. Still, fair enough, answer edited. – terdon Jan 10 at 0:14
[ "${#1}${1#-s}" = "$((${2%%*[!0-9]*}0?2:-1))" ] &&
echo yes

...if you balance the comparison you can sometimes shorten the test.

I prefer case though:

case ${1#-s}:$2 in
(*:*[!0-9]*|*:) ;;
("${1:+:$2}")   echo yes.
esac

Basically the idea is to rule out any match for a non-digit. For example:

[ "${2:+1$2}" = "1${2##*[!0-9]*}" ] && 
echo '"$2"' contains at least one character which \
         is a digit and zero which are not digits.

It's much more simple with case because you have multiple branches.

case $1:$2 in
(*:*?"$2")
    echo '"$1"' contains a "':'";;
(*:*[!0-9]*|*:0*[89]*|*:)
    echo '"$2"' either contains a not-digit character, no characters, \
           or is an invalid octal constant - or perhaps a mix-and-match;;
([!-]*|?[!s]*|??[!:]*)
    echo '"$1"' is either empty, doesn\'t match '-s' in its first two characters, \
          or is more than two characters. Or perhaps a mix-and-match.;;
(*) echo there are no more possibilities other than the one you want.
    echo but you can get here a lot sooner, as demonstrated above.
esac
share|improve this answer

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