Checking if a word starts with the letter B

I would like to check if the value I am testing starts with the letter B.I tried
 if [ ^$val ==?B? ]; then
         echo ?start with B?
else
        echo ?Does not start with B?
fi
But this does not work, it always goes to the else part even though the val is a string  starting with B

lubna132Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
griesshConnect With a Mentor Commented:
try that for a ksh:

#This extracts the first character
variable = `echo $val | cut -c 1`

if [ "$variable" = "B" ] ; then
echo "starts with B"
else
echo "does not start with B"
fi

BTW: there are more cryptic ways to get that result :-)

======
Werner
0
 
lubna132Author Commented:
I tried if [ $val = ^B ] this does not work as well
0
 
ahoffmannCommented:
# works in any UNIX and in any shell
if [ `echo "$var"|grep ^B` ]; then
  echo match
else
  echo no match
fi
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ecwCommented:
Bourne shell at its derivitives ...
 case "$var" in
   B*) echo "starts with a B" ;;
   *)  echo "does not start with a B" ;;
 esac
0
 
tfewsterCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation for this question in the Cleanup topic area as follows:
- Answered by griessh

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster (I don't work here, I'm just an Expert :-)
0
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0
All Courses

From novice to tech pro — start learning today.