Find Length of Shell (Bash) Variable
I was building an automation and needed to find the length of the variable. After doing some research, I found that you can do that easily with the commands below.
$ myvar="some string"
$ size=${#myvar}
$ echo "$size"
11
First line creates and sets the variable.
Second line gets the length of the variable and sets it into a variable named "size"
Third line displays the length of the variable. Once you have the length of the variable set into a variable, then the length variable can be used in if conditions, for statements, or anywhere else that you can use a variable.
Posted: 2022-09-17
Author: Kenny Robinson, @almostengr