D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

RE: [LUG] Bash substrings



Silly me!  I figured it out.  ${var:-value} is used for default setting, and
Bash will not know the difference with a substring.  Putting a space between
colon and hyphen works: ${var: -4}.

Clive 

-----Original Message-----
From: Darke, Clive
To: 'list@xxxxxxxxxxxx'
Sent: 16/02/2004 12:33
Subject: [LUG] Bash substrings

Any BASH experts out there?

I am looking at substrings, and do not seem to be able to get negative
offsets to work the way the man pages say/imply - 
"If  offset  evaluates  to  a number less than zero, the value is
used as an offset from the end of the value  of  parameter."   

${var:offset:length}  works fine if all are positive
${var:-3:3}  I thought would give me that last 3 chars of $var, but
gives me
the whole string.
${var:3,-3} Note the comma.  This DOES work (found by accident), but I
can't
find this syntax documented. 

Incidently (in this last example) it does not matter what number I have
as
the offset - even beyond the length of $var - but I must have one.

Any ideas? What is going on here?

Clive

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.



Lynx friendly