How can I count the number of characters in a Bash variable
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
Count occurrences of a char in a string using Bash
I would use the following awk
command:
string="text,text,text,text"
char=","
awk -F"${char}" '{print NF-1}' <<< "${string}"
I'm splitting the string by $char
and print the number of resulting fields minus 1.
If your shell does not support the <<<
operator, use echo
:
echo "${string}" | awk -F"${char}" '{print NF-1}'
Length of string in bash
UTF-8 string length
In addition to fedorqui's correct answer, I would like to show the difference between string length and byte length:
myvar='Généralités'
chrlen=${#myvar}
oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#myvar}
LANG=$oLang LC_ALL=$oLcAll
printf "%s is %d char len, but %d bytes len.\n" "${myvar}" $chrlen $bytlen
will render:
Généralités is 11 char len, but 14 bytes len.
you could even have a look at stored chars:
myvar='Généralités'
chrlen=${#myvar}
oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#myvar}
printf -v myreal "%q" "$myvar"
LANG=$oLang LC_ALL=$oLcAll
printf "%s has %d chars, %d bytes: (%s).\n" "${myvar}" $chrlen $bytlen "$myreal"
will answer:
Généralités has 11 chars, 14 bytes: ($'G\303\251n\303\251ralit\303\251s').
Nota: According to Isabell Cowan's comment, I've added setting to $LC_ALL
along with $LANG
.
Length of an argument, working sample
Argument work same as regular variables
showStrLen() {
local bytlen sreal oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#1}
printf -v sreal %q "$1"
LANG=$oLang LC_ALL=$oLcAll
printf "String '%s' is %d bytes, but %d chars len: %s.\n" "$1" $bytlen ${#1} "$sreal"
}
will work as
showStrLen théorème
String 'théorème' is 10 bytes, but 8 chars len: $'th\303\251or\303\250me'
Useful printf
correction tool:
If you:
for string in Généralités Language Théorème Février "Left: ←" "Yin Yang ☯";do
printf " - %-14s is %2d char length\n" "'$string'" ${#string}
done
- 'Généralités' is 11 char length
- 'Language' is 8 char length
- 'Théorème' is 8 char length
- 'Février' is 7 char length
- 'Left: ←' is 7 char length
- 'Yin Yang ☯' is 10 char length
Not really pretty output!
For this, here is a little function:
strU8DiffLen() {
local charlen=${#1} LANG=C LC_ALL=C
return $(( ${#1} - charlen ))
}
or written in one line:
strU8DiffLen() { local chLen=${#1} LANG=C LC_ALL=C;return $((${#1}-chLen));}
Then now:
for string in Généralités Language Théorème Février "Left: ←" "Yin Yang ☯";do
strU8DiffLen "$string"
printf " - %-$((14+$?))s is %2d chars length, but uses %2d bytes\n" \
"'$string'" ${#string} $((${#string}+$?))
done
- 'Généralités' is 11 chars length, but uses 14 bytes
- 'Language' is 8 chars length, but uses 8 bytes
- 'Théorème' is 8 chars length, but uses 10 bytes
- 'Février' is 7 chars length, but uses 8 bytes
- 'Left: ←' is 7 chars length, but uses 9 bytes
- 'Yin Yang ☯' is 10 chars length, but uses 12 bytes
Unfortunely, this is not perfect!
But there left some strange UTF-8 behaviour, like double-spaced chars, zero spaced chars, reverse deplacement and other that could not be as simple...
Have a look at diffU8test.sh or diffU8test.sh.txt for more limitations.
Count the number of digits in a bash variable
Assuming the variable only contains digits then the shell already does what you want here with the length Shell Parameter Expansion.
$ var=012
$ echo "${#var}"
3
Counting the Number of Characters in a File Using Bash and Assigning to Variable
The file will only contain one word
In that case you can do:
# read the whole file in a variable
w="$(<"$FILE")"
# get the length
echo "length is: ${#w}"
Option 2: Using wc
len=$(( $(wc -c < "$FILE") - 1 ))
Counting number of characters in a file through shell script
This will do it for counting bytes in file:
wc -c filename
If you want only the count without the filename being repeated in the output:
wc -c < filename
This will count characters in multibyte files (Unicode etc.):
wc -m filename
(as shown in Sébastien's answer).
Related Topics
Limiting Certain Processes to CPU % - Linux
How to Delete Many 0 Byte Files in Linux
Ubuntu - Run Command on Start-Up with "Sudo"
One Command to Create a Directory and File Inside It Linux Command
Refresh Net.Core.Somaxcomm (Or Any Sysctl Property) for Docker Containers
Linux's Thread Local Storage Implementation
Which Signal Does Ctrl-X Send When Used in a Terminal
How to Wait for Server Restart Using Ansible
How to Start a Shell Without Any User Configuration
Cmd 2>&1 > Log VS Cmd > Log 2>&1
How Set Multiple Env Variables for a Bash Command
Exclude List of Files from Find
Error: Gdal-Config Not Found While Installing R Dependent Packages Whereas Gdal Is Installed
Git Enter Long Passphrase for Every Push
Combine a Shell Script and a Zip File into a Single Executable for Deployment
Using Find - Deleting All Files/Directories (In Linux ) Except Any One