Linux Command Reference ยท Bash Syntax
Bash substring and case expansion
Reference for Bash substring extraction with ${var:offset:length}, negative offsets, array slices, and case conversion with ^, ^^, , and ,, operators.
Bash can cut a piece out of a string and change its letter case without calling cut, tr or awk. Substring expansion takes an offset and an optional length. Case modification uses ^ and , to convert to upper and lower case.
Both work on variables, positional parameters and arrays, and both happen inside the shell, so they are fast in loops.
s="Hello, World" echo "${s:7:5}" # World echo "${s^^}" # HELLO, WORLD
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
${parameter:offset} ${parameter:offset:length} ${parameter^} ${parameter^^} ${parameter^^pattern} ${parameter,} ${parameter,,} ${parameter,,pattern} ${parameter~} ${parameter~~}
| Part | Description |
|---|---|
offset |
Zero-based start position. Negative counts from the end, but needs a space or parentheses: ${s: -3} or ${s:(-3)}, otherwise :- is read as a default value. |
length |
Number of characters to take. A negative length stops that many characters before the end. |
^, ^^ |
Convert the first character / all characters to upper case. |
,, ,, |
Convert the first / all characters to lower case. |
~, ~~ |
Toggle the case of the first / all characters (rarely used; declare -u and -l are the attribute-based alternatives). |
pattern |
Optional glob; only characters matching it are converted. |
Returns: the resulting string. Edge cases: an offset past the end gives an empty string. For positional parameters, ${@:0} includes $0, and the offset counts parameters, not characters.
Usage notes
- Counting is in characters, not bytes, if your locale is UTF-8. In the C locale each byte counts as one character, so non-ASCII text can be cut in the middle of a character.
- Always put a space before a negative offset.
${s:-3}is the default-value expansion, not a slice. ${var:offset:length}with array expansion slices elements, not characters:${arr[@]:1:2}is two elements starting at index 1.- Case conversion depends on the locale. Letters outside ASCII (for example accented ones) only change when the locale supports them; with
LC_ALL=Cthey stay unchanged. - Older code uses
trorawk(tr a-z A-Z). The built-in form needs Bash 4.0 or later and does not exist inshordash. - Use it for comparisons. Lowering both sides with
,,is cleaner than enablingnocasematchglobally. - Zero-length and unset variables just give empty strings;
set -ustill errors for unset ones. - Not a regex tool. For pattern-based cutting, use trimming and replacement or regex matching.
Related
- Parameter expansion,
tr,cut, Indexed arrays - Lessons: Parameter expansion, Arrays