Write a program called valid that prints yes if its argument is a legal shell variable name and no otherwise:

$ valid foo_bar
yes
$ valid 123
no

if [ $# -ne 1 ] ; then
echo "Error: no variable name to test"
exit 1
fi

if [ -n "$(echo $1 | grep -E '^[a-zA-Z][a-zA-Z0-9_]{0,}$')" ] ; then
echo "yes"
else
echo "no"
fi

Note this works because the regular expression defines the valid language of a shell variable as an upper or lower case letter followed by zero or more alphanumeric values or an underscore. To work properly note you also need to use ^ to left root the expression to the beginning of the word and use $ to get the end of the expression, making sure the entire word given meets the regular expression's rules.

Computer Science & Information Technology

You might also like to view...

Create a UML class diagram for the ProductCodes program.

What will be an ideal response?

Computer Science & Information Technology

The ____ constraint schedules the Start date of the task on or before the date that you specify.

A. Start No Later Than B. Must Start On C. As Late As Possible D. Start No Earlier Than

Computer Science & Information Technology