5
#check if the name is valid
function myfunc()
{
    #check "${1}"
    #echo "valid/invalid"
}

#these should return valid
myfunc "my_number"
myfunc "my_number1"

#these should return ivalid 
myfunc "1my_number"
myfunc "1my _number"
myfunc "my number"
myfunc "my_number?"

and so on the variable name can have only letters , numbers (but not on the beginning),.. and like all the rules for java...

Is there any function that I can use ? I do not want to reinvent the wheel...

1
  • 1
    Why do you think there would be a built in function in bash to check against variable name rules for Java? Different languages have different restrictions on what is allowed in a variable name. You are going to need to determine the rules for your language and environment yourself, and implement it with something like the regex that @dogbane suggested. Commented Nov 8, 2012 at 17:52

4 Answers 4

7

Match the variable name against a regex, like this:

myfunc() {
    if [[ "$1" =~ ^[a-z][a-zA-Z0-9_]*$ ]]
    then
        echo "$1: valid"
    else
        echo "$1: invalid"
    fi
}
Sign up to request clarification or add additional context in comments.

3 Comments

^[a-z][a-zA-Z0-9_]*$ would mark variables starting with an uppercase character invalid. May I suggest ^[a-zA-Z][a-zA-Z0-9_]*$?
I did that on purpose. Java convention is not to start variables with an uppercase, but it's up to the OP to decide. It wasn't in his test cases.
It's also legal to begin a varname with underscore, so ^[_[:alpha:]][_[:alnum:]]*$ If you want to match case-insensitively, you can shopt -s nocasematch
3

dogbane's answer is almost complete for the context of bash variables, but it has not been updated to reflect the final comment which contains a fully working validator. According to his comment on his answer, this is intended. This answer provides a function which evaluates to true for all valid names and can be used as a condition rather than returning a value that must then be compared to something. Plus, it can be used across multiple shells.

 
The function:

isValidVarName() {
    echo "$1" | grep -q '^[_[:alpha:]][_[:alpha:][:digit:]]*$'
}

 
Example usage in bash:

key=...
value=...

if isValidVarName "$key"; then
    eval "$key=\"$value\""
fi


# or it might simply look like this

isValidVarName "$key" && eval "$key=\"$value\""

3 Comments

Why the " && return || return 1" part? The return code of grep seems correct to me.
@OlivierPirson, you make an excellent point. I have no idea why I did that. Updating my answer.
May I ask if there are any particular reasons not using [:alnum:] instead of [:alpha:][:digit:]? For instant I suggest '[[:alpha:]_][[:alnum:]_]*$' which is more concise.
0

Bash only:

isvalidvarname ()
{
    local varname;
    local regexp_varname='^[_[:alpha:]][_[:alpha:][:digit:]]*$';
    varname="$1";
    [[ "${varname}" =~ ${regexp_varname} ]]
}

Comments

0

A combination of already proposed answers. For all shells, by using grep:

is_valid_name() {  # NAME
    printf '%s' "$1" | grep --quiet '^[_[:alpha:]][_[:alpha:][:digit:]]*$'
}

For shells that accept regexp, like Bash:

function is_valid_name {  # NAME
    [[ "$1" =~ ^[_[:alpha:]][_[:alpha:][:digit:]]*$ ]]
}

Both return 0 (true) if NAME is a valid variable name, else return 1 (false). They can be used like this:

NAME='...'
is_valid_name "$NAME" && ...
if is_valid_name "$NAME"; then
    ...
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.