Concept Flow - String length (${#var})
Start with variable
Apply ${#var}
Calculate length
Return length as number
Use or display length
This flow shows how bash calculates the length of a string stored in a variable using ${#var}.
name="hello" length=${#name} echo $length
| Step | Variable | Action | Value | Output |
|---|---|---|---|---|
| 1 | name | Assign 'hello' | hello | |
| 2 | length | Calculate ${#name} | 5 | |
| 3 | echo $length | Print length | 5 | 5 |
| Variable | Start | After 1 | After 2 | Final |
|---|---|---|---|---|
| name | hello | hello | hello | |
| length | 5 | 5 |
Use ${#var} to get the length of the string in var.
It returns the number of characters as a number.
If var is empty or unset, length is 0.
Example: name="hi"; echo ${#name} outputs 2.