Testing for strings, and numbers

Chris G cl at isbd.net
Fri Nov 13 14:20:03 UTC 2009

On Fri, Nov 13, 2009 at 03:14:36PM +0100, Nils Kassube wrote:
> 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).
If you have python installed then I'd advise using that, *much* easier
than bash (or other shells) for string operations like this.  I use
python for just about everything except really trivial little scripts.

Chris Green

More information about the ubuntu-users mailing list