This is the P2PU Archive. If you want the current site, go to!

Scripting 101

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Questions About: Bash

Go back to: General discussion

So as I was messing around with variables in Bash, a question popped into my head. As I was about to post it in the forum, I decided it would probably be best if we had separate topics on each of the languages were working on concerning specific questions we have about the language. It would also be a nice place to offer general tips and tricks on working with the languages as well. In any case, on to my question!

So, I was messing around with the declare command. In my case, I declared -i to limit a variable to stay limited to integers. When I try to assign it a string, and out the variable, it displays 0. My question is: Why does it display 0? Hoping somebody could shed some light on that curiosity for me. :D

hemanth hm's picture
hemanth hm
Thu, 2010-09-23 06:49

Nice move Adam,
An Integer data type which is declared with declare -i, makes that variable to take only integer values, that is not fractions.
So when one tries to assign invalid data type it, it would be internally assigned to ZERO, to makes things easier, why? One might ask?
Lets take an example :
Assume you have defined a variable called number, as declare -i number, then you assign number="Testing" or you read it from the user as read -p "Enter a number:" number and he enter a string hoping that he will break the code, but the smart guy in the example has declared it with declare -i, now it very easy to validate [[ $y ]] && echo Enter a positive number that is non-zero
the data you have entered is $y

Adam Collado's picture
Adam Collado
Thu, 2010-09-30 02:53

I'm running into an issue with my case statements in bash.

Basically, I was trying to be fancy, and decided to make a case statement where it asks for your age and determines what category of age you fall under. It's going good for the most part, until I try to determine an age that [number] and greater. Heres my code:

read -p "How old are you? " age
echo -n "Based on your age range, your: "
case $age in
0-11) echo -n "A Boy or Girl!";;
12-13) echo -n "A Boy or Girl going through Puberty!";;
14-18) echo -n "A teenager!";;
19-59) echo -n "A man or woman!";;
60-) echo -n "An old man or woman!";;
*) echo -n "I have no clue what the hell you are....";;

The line of note is the "60-) echo -n "An old man or woman!";;"
Basically, I don't know how to say "60 and up" in bash. Is there a way to resolve this?

hemanth hm's picture
hemanth hm
Thu, 2010-09-30 16:52

Sorry for the delay in reply, first of all case generic form is

case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac

WORD matching PATTERN is what is not be noticed here. So its a PATTERN that is matched not a range, expressing range as a pattern is very obfuscated and not entertained. To do something related to range its better to use a if else construct i.e if you're doing comparisons of numeric (integer) ranges, then yes, use if and ((...))