checking for hexadecimal vals only in a string in bash

Fred Smith fredex at fcshome.stoneham.ma.us
Thu Sep 18 22:34:34 UTC 2014


On Thu, Sep 18, 2014 at 04:31:03PM -0500, Chris Kottaridis wrote:
> In a bash shell script I want to see if a string has nothing but
> hexadecimal values in it.
> 
> So:
> 
> A098FE or af098fe
> 
> should be true
> 
> hello
> 
> should not.
> 
> How do  I check for that ?
> 
> I've been playing with
> 
> if [[ $val =~ '/^[A-Fa-f0-9]+$/' ]] ; then
>    echo is hex
> else
>    echo is not hex
> fi
> 
> I've tried various incantations of above  and also tried using grep
> but can't seem to get it just right.
> 
> Any help appreciated.

Have you looked in the Advanced Bash Scripting guide? I dunno if
it discusses that particular thing, but there is a lot of tricky
stuff there that may help you.

http://tldp.org/LDP/abs/html/

-- 
---- Fred Smith -- fredex at fcshome.stoneham.ma.us -----------------------------
   "For the word of God is living and active. Sharper than any double-edged 
   sword, it penetrates even to dividing soul and spirit, joints and marrow; 
              it judges the thoughts and attitudes of the heart."  
---------------------------- Hebrews 4:12 (niv) ------------------------------


More information about the users mailing list