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

Andy Green andy at warmcat.com
Mon Oct 8 06:34:47 UTC 2007


Somebody in the thread at some point said:
>   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.

How about

if [ "${VAR:0:1}" = "/" ] ; then

-Andy




More information about the users mailing list