Bash command to check if the variable name is valid
Asked Answered
U

4

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

Underlayer answered 8/11, 2012 at 17:16 Comment(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.Innocency
A
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
}
Ascertain answered 8/11, 2012 at 17:24 Comment(3)
^[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_]*$?Verdin
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.Ascertain
It's also legal to begin a varname with underscore, so ^[_[:alpha:]][_[:alnum:]]*$ If you want to match case-insensitively, you can shopt -s nocasematchRoundabout
R
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\""
Rutkowski answered 15/3, 2019 at 1:13 Comment(3)
Why the " && return || return 1" part? The return code of grep seems correct to me.Nighttime
@OlivierPirson, you make an excellent point. I have no idea why I did that. Updating my answer.Rutkowski
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.Nymphalid
E
0

Bash only:

isvalidvarname ()
{
    local varname;
    local regexp_varname='^[_[:alpha:]][_[:alpha:][:digit:]]*$';
    varname="$1";
    [[ "${varname}" =~ ${regexp_varname} ]]
}
Edacity answered 16/1, 2023 at 22:56 Comment(0)
N
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
Nighttime answered 1/7, 2023 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.