Prima di tutto, si noti che quando si dice questo:
BAR=$(basename $FOO) # result is BAR="baz"
BAZ=${BAR:0:1} # result is BAZ="b"
il primo bit nel costrutto per BAZ
è BAR
e non il valore di che si vuole prendere il primo carattere di. Quindi, anche se bash permettesse che i nomi delle variabili contengano caratteri arbitrari, il risultato nella seconda espressione non sarebbe quello che vuoi.
Tuttavia, per quanto riguarda la regola che impedisce questo, mi permetta di citare dalla pagina man di bash:
DEFINITIONS
The following definitions are used throughout the rest of this docu‐
ment.
blank A space or tab.
word A sequence of characters considered as a single unit by the
shell. Also known as a token.
name A word consisting only of alphanumeric characters and under‐
scores, and beginning with an alphabetic character or an under‐
score. Also referred to as an identifier.
Poi un po 'più tardi:
PARAMETERS
A parameter is an entity that stores values. It can be a name, a num‐
ber, or one of the special characters listed below under Special Param‐
eters. A variable is a parameter denoted by a name. A variable has a
value and zero or more attributes. Attributes are assigned using the
declare builtin command (see declare below in SHELL BUILTIN COMMANDS).
E più tardi, quando si definisce la sintassi stai chiedendo informazioni su:
${parameter:offset:length}
Substring Expansion. Expands to up to length characters of
parameter starting at the character specified by offset.
Quindi le regole come articolate nella manpage dicono che loIl costruttodeve avere un parametro come prima parte e che un parametro può essere solo un nome, un numero o uno dei pochi caratteri di parametro speciali. $(basename $FOO)
non è una delle possibilità consentite per un parametro.
Per quanto riguarda un modo per eseguire questa operazione in un compito, utilizzare una pipe per altri comandi come indicato in altre risposte.
fonte
2011-05-06 22:39:40