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

Todd Zullinger tmz at pobox.com
Mon Oct 8 07:03:02 UTC 2007


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.

You could use the =~ operator in bash:

if [[ "$VAR" =~ ^/ ]] ; then ...

You could also use expr:

if expr "/path" : ^/ >/dev/null; then ...

And I'm sure there are better ways than these.

-- 
Todd        OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Most of one's life is one prolonged effort to prevent oneself
thinking.
    -- Aldous Huxley

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 542 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20071008/f8d704db/attachment-0001.sig>


More information about the fedora-list mailing list