Measuring a value
The expansion ${#name} gives the number of characters in the value. It is the shell equivalent of wc -c for a string, without the trailing newline that a pipe would add. For an array, the count form is ${#name[@]}, as the array unit explained. An unset variable has length zero.
s="Hello, World"
echo "${#s}"The output is 12. The comma and the space are two of the characters, which is why the number is larger than the word count.
Substrings by position
The form ${name:offset:length} returns length characters starting at offset. Offsets start at zero. If you leave out the length, the result runs to the end of the value. A negative offset counts from the end, but it must be separated from the colon by a space or be in parentheses, because the colon followed by a minus sign would be read as the default form.
s="Hello, World"
echo "${s:7:5}"
echo "${s: -5}"
echo "${s:0:5}"The output is World, World, and Hello. The first line starts at position seven, which is the W, and takes five characters. The second line takes the last five characters. The space before the minus sign is what makes the negative offset work.
Write ${s: -5} with the space. Without it, ${s:-5} means a default value of 5 if s is empty, which is a different expansion entirely and does not fail loudly.
Changing case
Bash 4 introduced case conversion in parameter expansion. A caret converts to upper case and a comma converts to lower case. One symbol changes only the first character, and two symbols change every character. The tilde form toggles the case of each letter.
w=hELLO
echo "${w^}"
echo "${w^^}"
echo "${w,,}"
echo "${w~~}"The output is HELLO, HELLO, hello, and Hello. The single caret changes only the first character, which is already upper case in the sense that matters here, and the rest is left alone. Because the rest of the value is already capitals, the single and double caret look the same in this example. The tilde form swaps the case of every letter, so the h becomes H and the ELLO becomes ello.
Case conversion makes comparisons easier. Normalize both sides to lower case before you compare them, so that a user typing YES and a file containing yes are treated as the same answer.
Using length to guard a loop
A common use of the length form is to skip empty input. A loop that reads a value and processes it should check that the value is not empty before it does any work, and the length expansion is the clearest way to write that test.
for item in "" alpha "" beta; do
if [ "${#item}" -eq 0 ]; then continue; fi
echo "item=$item"
doneThe output is item=alpha and item=beta. The empty strings were skipped without any external command, and the test reads like what it does.
Characters versus bytes
In a UTF-8 locale, the length and substring forms count characters, so a multibyte letter counts as one. In the C locale they count bytes. The lab machine uses a UTF-8 environment, but scripts that must behave the same everywhere should set LC_ALL explicitly when the difference matters. For plain ASCII text, the two agree.
Putting the forms together
A common task is to take the first few characters of an identifier and make them upper case for a label. The forms combine cleanly because each one returns a plain string that the next one can work on.
id=deployment42
label="${id:0:4}"
echo "${label^^}-${#id}"The output is DEPL-12. The first four characters were cut, converted to upper case, and joined to the length of the original identifier.
Hands-on: measure and cut
Run the block below and predict the output. The lab for this unit asks you to write the same expansions into a script and check the lines it prints. Try changing the offsets to see how the results change.
word="shellscript"
echo "${#word}"
echo "${word:5}"
echo "${word: -6:3}"The output is 11, then cript, then scr. The third line counts six characters back from the end, which is the s of script, and takes three characters from there.