96

How can I count all characters in a bash variable? For instance, if I had

"stackoverflow"

the result should be

"13"
codeforester
  • 39,467
  • 16
  • 112
  • 140
lacrosse1991
  • 2,972
  • 7
  • 38
  • 47
  • 3
    One option is [`wc`](http://linux.about.com/library/cmd/blcmdl1_wc.htm). – Aiias Mar 24 '13 at 07:39
  • 3
    **using wc and tr together** `echo stackoverflow|tr -d '\n' | wc -c` – pulkit219 Apr 28 '20 at 15:33
  • Take `function char-count() { echo -n "$1" | wc -c | awk '{print $1}' }`, save it in your .zshrc and use it: `char-count "hello world"` and you get **11** since `${#VAR}` is not working on macOS UNIX zsh. – shogitai Feb 04 '21 at 00:35
  • @Aiias Broken link. linux.about.com redirects to www.lifewire.com – Naeio Oct 06 '22 at 10:02

5 Answers5

125

Using the ${#VAR} syntax will calculate the number of characters in a variable.

https://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

SteveP
  • 18,840
  • 9
  • 47
  • 60
  • 3
    Link could die. `${#parameter}` gives the length in characters of the expanded value of parameter is substituted. If parameter is `*` or `@`, the value substituted is the number of positional parameters. If parameter is an array name subscripted by `*` or `@`, the value substituted is the number of elements in the array. If parameter is an indexed array name subscripted by a negative number, that number is interpreted as relative to one greater than the maximum index of parameter, so negative indices count back from the end of the array, and an index of -1 references the last element. – Alessandro Flati Mar 25 '20 at 08:58
  • Bash doesn't properly count unicode characters or other multibyte characters. – deanresin May 02 '21 at 00:15
88

Use the wc utility with the print the byte counts (-c) option:

$ SO="stackoverflow"
$ echo -n "$SO" | wc -c
    13

You'll have to use the do not output the trailing newline (-n) option for echo. Otherwise, the newline character will also be counted.

coolaj86
  • 74,004
  • 20
  • 105
  • 125
mihai
  • 4,592
  • 3
  • 29
  • 42
  • 4
    You don't need echo. If going for `wc` do `wc -c <<< $SO` – jacwah Apr 02 '16 at 16:18
  • @jacwah: this will also count the trailing newline (so answer will be 14 and not 13). – gniourf_gniourf Nov 26 '17 at 14:07
  • 6
    Use -m for chars, not -c which is for bytes – raacer Dec 29 '18 at 01:28
  • 2
    echo adds a newline unless you use the -n flag, however not all implementations of echo support the -n flag. use printf for a more consistent result – Mr Griever Feb 01 '20 at 16:50
  • 3
    please note that using (admittedly cool) here-string solution suggested by @jacwah you end up counting one character more, because bash always adds a newline to the end of the string. – gerlos Feb 12 '20 at 11:47
33
jcomeau@intrepid:~$ mystring="one two three four five"
jcomeau@intrepid:~$ echo "string length: ${#mystring}"
string length: 23

link Couting characters, words, lenght of the words and total lenght in a sentence

kos
  • 1,770
  • 2
  • 19
  • 41
Raj
  • 517
  • 3
  • 10
17
${#str_var}  

where str_var is your string.

aga
  • 27,954
  • 13
  • 86
  • 121
2

you can use wc to count the number of characters in the file wc -m filename.txt. Hope that help.