This reads from stdin and reports on true or false status (and exits if it is false):
grep -q '^[a-z_][a-z0-9_-]*$' && echo true || { echo false; exit 1 ; }
If grep finds a match to your regex, it sets its exit code to true (0) in which case the "and" (&&) clause is executed and "true" is returned. If grep fails to find a match, the "or" (||) clause is executed and "false" is returned. The -q flag to grep tells grep to be quiet.
If one were to use this in a script, one would probably want to capture the user input into a shell variable and then test it. That might look like:
read -p "Enter a name: " var
echo "$var" | grep -q '^[a-z_][a-z0-9_-]*$' && echo true || { echo false; exit 1 ; }
To make it easy to add more statements to execute if the result is "true", we can write it out in a longer form with a place marked to add more statements:
read -p "Enter a name: " var
if echo "$var" | grep -q '^[a-z_][a-z0-9_-]*$'
then
echo true
# other statements to execute if true go here
else
echo false
exit 1
fi