bash: how to check if first character of string is "/"?

Robert P. J. Day rpjday at mindspring.com
Mon Oct 8 07:01:50 UTC 2007


On Mon, 8 Oct 2007, Ralf Corsepius wrote:

> On Mon, 2007-10-08 at 02:23 -0400, Robert P. J. Day wrote:
> > it's late so i'm sure i'm just being dense in not knowing this, but
> > is there a simpler way to check if the first character of a string is
> > a "/" rather than the cumbersome:
> >
> >   if [ "x${VAR##/*}" = "x" ] ; then ...
> >
> > surely there must be *something* that doesn't look quite so grotesque.
> If you want it portable/bourne-shell compatible, consider "case"
>
> case "$VAR" in
> /*)
>    ...;;
> esac

i knew about that variation, but it seemed like overkill for a simple
one-character comparison.  andy's version seems like the simplest
solution so far.

rday
-- 
========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry
Waterloo, Ontario, CANADA

http://crashcourse.ca
========================================================================




More information about the users mailing list