1 |
On Thu, 17 Sep 2009 09:04:22 +0800 |
2 |
Xi Shen <davidshen84@××××××××××.com> wrote: |
3 |
|
4 |
> i searched for a while, but cannot find the reference of that syntax. |
5 |
> thanks a lot. |
6 |
> |
7 |
|
8 |
The ordinary syntax is ${string:position:length}, but in the ordinary case |
9 |
the position is referenced from the LEFT end of the string. To reference |
10 |
from the RIGHT end of the string, as in the Gentoo script, a negative value |
11 |
must be specified, but the negative value has to the result of an EXPRESSION, |
12 |
hence the apparently unusual syntax. |
13 |
|
14 |
Why? See question E12 at this link: |
15 |
http://www.faqs.org/faqs/unix-faq/shell/bash/ |
16 |
|
17 |
Just try it out. Copy the following two lines to a file, give the file |
18 |
executable permission, and create different symbolic links to it. |
19 |
Then invoke the file using any of the symlinks. |
20 |
|
21 |
#! /bin/bash |
22 |
echo ${0:$[-2]} |
23 |
|
24 |
|
25 |
Frank Peters |