Linux and Networking

Linux and Networking

4.22. Length of a string.

Now we are going to see how to work with character strings within shell script.

 

To know the length of a string we have three options:

 

${#string}

expr length $string

expr “$string” : ‘.*’

 

The most used options are the first two while the third, in addition to not being very intuitive, is also not widely used by shell script programmers.

 

Let’s see an example of using these functions

 

$stringMY=ArrozConSocarrat

$echo ${#stringMY}

16

$echo `expr length $stringMY`

16

 

As an exercise, we suggest that you execute the three commands in the previous code.

 

The Demeter Project