まず、これを言うときは次のことに注意してください。
BAR=$(basename $FOO) # result is BAR="baz"
BAZ=${BAR:0:1} # result is BAZ="b"
for コンストラクトの最初のビットは、最初の文字を取得する値BAZ
ではBAR
ありません。そのため、bash が変数名に任意の文字を含めることを許可したとしても、2 番目の式の結果はあなたが望むものにはなりません。
ただし、これを妨げているルールについては、bash の man ページから引用させてください。
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.
それから少し後:
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).
そして後で、あなたが求めている構文を定義するとき:
${parameter:offset:length}
Substring Expansion. Expands to up to length characters of
parameter starting at the character specified by offset.
そのため、マンページに明確に示されている規則では、${foo:x:y}
構造体は最初の部分としてパラメーターを持たなければならず、パラメーターは名前、数値、またはいくつかの特殊なパラメーター文字の 1 つだけにすることができます。$(basename $FOO)
パラメーターに許可されている可能性の 1 つではありません。
1 つの割り当てでこれを行う方法については、他の応答で述べたように、他のコマンドへのパイプを使用します。