This page looks best with JavaScript enabled

Counting Characters in a Bash String - Quick Tip

 ·   ·  ☕ 1 min read

Bash has a series of Shell Parameter Expansion features that allow you to do a number of different things when working with your shell. One of those features makes it possible to get the number of characters in a string easily.

For example this stores “Hello World” into myvariable and then writes 11 to the screen (the number of characters in the string).

myvariable="Hello World"
echo "${#myvariable}"

You can read more about Bash’s features and Shell Parameter Expansion specifically in the Bash docs:

Join the World of Zero Discord Server:

Sam Wronski
Sam Wronski
Maker of things and professional software engineer. Lets make something awesome together!