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, currently helping build cloud things @ Google. World of Zero is a personal project disconnected from my professional work. Lets make something awesome together!