Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off, note that when you say this:</p> <pre><code>BAR=$(basename $FOO) # result is BAR="baz" BAZ=${BAR:0:1} # result is BAZ="b" </code></pre> <p>the first bit in the construct for <code>BAZ</code> is <code>BAR</code> and not the <em>value</em> that you want to take the first character of. So even if bash allowed variable names to contain arbitrary characters your result in the second expression wouldn't be what you want.</p> <p>However, as to the rule that's preventing this, allow me to quote from the bash man page:</p> <pre><code>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. </code></pre> <p>Then a bit later:</p> <pre><code>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). </code></pre> <p>And later when it defines the syntax you're asking about:</p> <pre><code> ${parameter:offset:length} Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. </code></pre> <p>So the rules as articulated in the manpage say that the <code>${foo:x:y}</code> construct must have a parameter as the first part, and that a parameter can only be a name, a number, or one of the few special parameter characters. <code>$(basename $FOO)</code> is not one of the allowed possibilities for a parameter.</p> <p>As for a way to do this in one assignment, use a pipe to other commands as mentioned in other responses.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload