← Back to Bash Course | Chapter 5: Functions | Lesson 1 of 11

String Length

In this page:

  1. String Length

String Length

${#variable} gives the number of characters in a string variable. It's a fast, built-in way to check string length without calling an external command like wc. This syntax is part of Bash's parameter expansion feature.

Note: Use ${#var} instead of piping through wc -c — it avoids starting a subprocess.

Example: String Length

bash
#!/bin/bash
name="Hello, Bash!"
echo "Length: ${#name}"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.