2
\$\begingroup\$

I test in a bash script if a variable value contains digits only. There are two options, both give the valid results:

validate_is_pos_int(){
local value=$1
if ! [[ ${value//[0-9]/""} ]]
then
    return 0 # True; $1 has digits only
else
    return 1 # False; $1 contains other letters besides digits
fi}

and the second (maybe more readable):

validate_is_pos_int(){
local value=$1
if [[ -z ${value//[0-9]/""} ]]
then
    return 0 # True; $1 has digits only
else
    return 1 # False; $1 contains other letters besides digits
fi}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.