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).

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

You can read more about Bash’s features and Shell Parameter Expansion specifically in the Bash docs: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion

Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u


Sam Wronski
WRITTEN BY
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!