How to get length of a string/integer variable in linux bash shell ?

Today in this post, we will look how to get length of a variable in linux whether its a string or integer.

Getting variable length in Linux

1. Using shell builtin $#.

### Lets declare a variable
bash-3.2# a=NGELinux
bash-3.2# echo $a
NGELinux

### Get its length by putting # in front of the variable
bash-3.2# echo ${#a}
8
bash-3.2# 

 

2. Using wc command.

bash-3.2# echo $a
NGELinux

bash-3.2# echo -n $a | wc -m
       8
bash-3.2# 

Above we have seen few methods to achieve this, however the same task can be achieved by many other ways.

Leave a Reply

avatar
  Subscribe  
Notify of