Testing for strings, and numbers

Nils Kassube kassube at gmx.net
Fri Nov 13 14:14:36 UTC 2009


Ray Parrish wrote:
> I am writing a bash script and I would like to test $1 to see if it
> begins with the string "http://" but I am not coming up with a method
>  of doing so.

echo "$1"|grep -q '^http://' && echo yes || echo no

> Also I would like to know how to test $1 to see if it begins with a
> numeric digit.

echo "$1"|grep -q '^[0-9]' && echo yes || echo no

> All of my attempts so far have been stymied, and I can't seem to find
> the excellent bash reference I had been using before, and am trying
>  to navigate a couple of less than excellent references that do not
>  include sections on string handling.

You can of course do it with bash string operations but I think grep is 
simpler here. I don't know which bash reference you mean but I prefer 
"learning the bash shell" by Cameron Newham & Bill Rosenblatt (O'Reilley 
book).


Nils




More information about the ubuntu-users mailing list