$_ CLI In Depth
Learning progress 0%
lesson functions arrays parameter-expansion

Length, Substrings & Case Conversion

4 min read Module 14: Functions, Arrays & Parameter Expansion Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:16

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.

BASH2 lines
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.

BASH4 lines
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.

Warning

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.

BASH5 lines
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.

BASH4 lines
for item in "" alpha "" beta; do
  if [ "${#item}" -eq 0 ]; then continue; fi
  echo "item=$item"
done

The 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.

BASH3 lines
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.

BASH4 lines
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.

Parameter expansion on one value

Visualizer

Parameter expansion changes the value of a variable without starting a subprocess. Each operator is a small pattern or rule applied to the same string.

1 / 6
path=/var/log/app.tar.gz
expressionresultmeaning
${path}/var/log/app.tar.gzthe value
${#path}19length in characters
${path#*/}var/log/app.tar.gzdrop shortest prefix matching */
${path##*/}app.tar.gzdrop longest prefix matching */ (basename)
${path%.*}/var/log/app.tardrop shortest suffix matching .*
${path%%.*}/var/log/appdrop longest suffix matching .*
${path/log/LOG}/var/LOG/app.tar.gzreplace first match
${path//./_}/var/log/app_tar_gzreplace every match
${path^^}/VAR/LOG/APP.TAR.GZuppercase everything
${path:1:3}varoffset 1, length 3

Start with the value. ${path} gives it back, and ${#path} counts its 19 characters, slashes and dots included.

path=/var/log/app.tar.gz; echo "${#path} ${path##*/} ${path%%.*} ${path//./_} ${path:1:3} ${path^^}"
Terminal lab · 2 steps · real Linux in your browser

Measure, cut and change case

Use length, substring and case conversion forms on short strings.

01
Measure and cut a string
Create text.sh in /workspace. Set s to Hello, World. Print its length, the substring of five characters starting at position seven, the last five characters, the whole string in upper case, and in lower case: ``bash cat > text.sh <<'EOF' s="Hello, World" echo "${#s}" echo "${s:7:5}" echo "${s: -5}" echo "${s^^}" echo "${s,,}" EOF `` Use the space before the minus sign for the last-five form.
Show hint for step 1
The offset is zero-based. The negative offset needs a space before the minus sign, or bash reads it as a default value.
02
Change the case of one word
Create case.sh in /workspace. Set w to hELLO. Print the value with only its first letter upper-cased, then print it with every letter toggled: ``bash cat > case.sh <<'EOF' w=hELLO echo "${w^}" echo "${w~~}" EOF ``
Show hint for step 2
One caret changes the first letter only. Two tildes swap the case of every letter.
Check yourself Question 1 of 4
Concept Check Length of an unset variable recall

What does echo "${#s}" print when s is unset?

Recap & Key Takeaways

${#name} gives the number of characters in a value, and an unset variable has length zero.
${name:offset:length} cuts a substring from a zero-based offset, and a negative offset needs a space before the minus sign.
${name^} and ${name^^} change to upper case, ${name,} and ${name,,} change to lower case, and ${name~~} toggles every letter.
Case conversion and length checks let scripts normalize input and skip empty values without starting external programs.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue