I've got several arguments ($1 $2) to a script from the command line.

I'd been trying to test them, to make sure that the values entered weren't junk:

if [ $1 ]; then
if [[ -n ${INPUT//[0-9]/} ]]; then

Ie: I'm trying to make sure that $1 is not empty, and composed of 0-9 (digits), ie someone didn't type script string more_strings or a plain, unargumented script instead of something like script 12 321

But then trying to treat $INPUT (I also tried $1) as an integer:

if [ $INPUT -gt 100 ]; then

Failed, and via echo testing, it looks like I've got a string (even though I used gt - which I thought meant to evaluate it as an integer?), and I'm not sure how to make it/evaluate $1 as an integer?


Suggests I do this:

if [[ $1 != *[!0-9]* ]]; then

But I don't understand what's going on in there. Not equal to not 0-9??? Why *?

This looked interesting:

declare -i $1

But seems to permanently change $1? until reassigned?

I'd like to know how to test my inputs, to make sure they're either A) strings or B) numbers/integers - and then how to use them from within my script (or how to convert them). Preferably with an explanation.

I've tried doing some searches of prior questions, and I'm obviously searching on the wrong terms - as this seems like a very rudimentary thing that I've forgotten how to do/should have already been answered somewhere else.

Didn't find it here, either:


Maybe bedtime is needed.


Within [[ .. ]], the = operator is actually a pattern match, so [[ $1 != *[!0-9]* ]] tests if the value of $1 does not match the pattern *[!0-9]*.

The patterns used here are the same as those for filename expansion, so * matches any character, and [abc] any character between the brackets, and [!abc] or [^abc] any character not between the brackets (excluding the initial ! or ^). In effect, that tests if $1 contains some character that is not a digit from 0 to 9.

declare -i var would mark the variable var as an integer variable: assignments to it would be interpreted as in an arithmetic context, so declare -i a; a=3+5; echo $a; would output 8. But you can't use that on a positional parameter, like $1. Instead declare -i $1 would take the string contained in $1 and use that as a variable name. (Note that there's no $ in declare -i a above.)

You can't really use declare -i to test if something is a number, as strings in arithmetic context are taken as variable names. This isn't an error: declare -i a; b=1+2; a=b; echo $a;. Instead, it prints 3, which may or may not be what you want.


Checking to see if there is input, and then if it is numerical (of 1 or more digits), I used this glob:

if [ $2 ]; then INPUT=$2 if [[ $INPUT = +([[:digit:]]) ]]; then

This appears to be working to test for non-numerical input:

if [[ -n ${INPUT//[0-9]/} ]]; then


Not a nice explanation like from ilkkachu

But you can try this way

[ ! -z $1 -a $1 -eq $1 ] 2>/dev/null && echo "integer $1" || echo "not integer $1"

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.