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...

Lukap
  • 31,523
  • 64
  • 157
  • 244
  • 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. – Brian Campbell Nov 08 '12 at 17:52

4 Answers4

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
}
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 7
    `^[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_]*$`? – sampson-chen Nov 08 '12 at 17:28
  • 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. – dogbane Nov 08 '12 at 17:31
  • 9
    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` – glenn jackman Nov 08 '12 at 20:56
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:]]*$' && return || return 1
}

 
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\""
b_laoshi
  • 443
  • 6
  • 12
0

Bash only:

isvalidvarname ()
{
    local varname;
    local regexp_varname='^[_[:alpha:]][_[:alpha:][:digit:]]*$';
    varname="$1";
    [[ "${varname}" =~ ${regexp_varname} ]]
}
Paxsali
  • 71
  • 1
  • 6
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
Olivier Pirson
  • 737
  • 1
  • 5
  • 24