Shell Scripting Tip: Find The Length Of a String Under UNIX / Linux Oses

by on August 29, 2007 · 21 comments· LAST UPDATED December 17, 2013

in , ,

While writing a shell script you may want to find out the length of a string. While reading GNU expr command man page I found an interesting option:

expr length STRING

For example display the length of "nixcraft" word/string, enter:

 
expr length "nixcraft"
 

Sample outputs:

8

expr and POSIX

Please note that the expr command is not concerned with POSIX (open system standards based on Unix). You can try old good KSH/SH/Bash command as follows which should work with any UNIX-likeo operating systems such as FreeBSD / Solaris/ AIX/HP-UX and so on:

 
myVar="nixcraft"
echo "${#myVar}"
 

Sample outputs:

8
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 21 comments… read them below or add one }

1 Corey Hart August 29, 2007 at 8:02 pm

Few more examples:

% echo nixcraft | awk ‘ { print length } ‘
8

% echo nixcraft | perl -nle ‘ print length ‘

Reply

2 nixCraft August 29, 2007 at 8:09 pm

Corey,

Nice awk / perl tips :)

Appreciate your post!

Reply

3 name December 17, 2008 at 6:50 pm

echo -n “hello world” | wc -c

Reply

4 crazysenses April 29, 2009 at 1:55 pm

Thanks for the tips
It was very useful for my work
Greetings from Mexico

Reply

5 hw May 10, 2009 at 8:38 pm

hi i tried this program but its not working in my computer lab.
kindly let me know what is wrong in the program
echo enter the string
read string
length = ‘expr length $string’
echo “length of the string is $length”

output should come as
eg:
enter the string:Harshi
length of the string is :6
but in the lab its coming as
enter the string :Harshi
length command cannot be read
please try to solve my problem
thank you

Reply

6 Sola July 21, 2010 at 9:09 am

to hw,

the correct code is:
echo enter the string
read string
length=$(expr length “$string”)
echo “length of the string is $length”

You must redirect the output into the variable, otherwise it don’t read the value.
Nice job.

Reply

7 sur March 15, 2011 at 6:33 am

i dunno y it is not working .have u given the inverted commas and stuffs??
this code worked for me..

echo ” enter thestring”
read str
len=`expr length $str`
echo “Length is $len”.

Reply

8 seerum June 25, 2009 at 10:10 am

use length=`expr length $string` it will work

Reply

9 Will July 3, 2009 at 12:49 am

I am trying to create a string that will allow me to set value a a particular position in the string.
I am running a validation an have many for loops with various variable lengths being returned.
I want the output to be formatted so that the status is lined up in the same postion.
for example i check a list of files to see if they exist and I want to display an OK or FAILED in the output. I want the output to look like something below.
/IDS

Reply

10 Will July 3, 2009 at 12:52 am

sorry I didnt finish what I wanted to explain.
I am trying to create a string that will allow me to set value a a particular position in the string.
I am running a validation an have many for loops with various variable lengths being returned.
I want the output to be formatted so that the status is lined up in the same postion.
for example i check a list of directories to see if they exist and I want to display an OK or FAILED in the output. I want the output to look like something below.
/IDS [ OK ]
/VALID [ OK ]
/DEVEL [FAILED]
/PROGRAMS [ OK ]

any help

Reply

11 Fox December 3, 2009 at 3:33 pm

Hey !

Thanks for the tip ! resolved my issue !
Expr lenght was working under linux but not under Solaris.

Reply

12 velmani February 17, 2014 at 2:17 pm

The command `expr length $var` is platform dependent.You can not run it in another platform rather than the Linux.But if you using the command `echo ${#var}`, you can run it in cross platform.

Reply

13 Patwari June 22, 2010 at 11:55 am

myVar=”nixcraft”
echo ${#myVar}
echo ${myVar} | awk ‘ { print length } ‘
echo “${myVar}” | wc -c
echo `expr length ${myVar}`

Reply

14 alhaizan July 6, 2010 at 9:43 pm

Another simple way is:

mystr=”A random string”
len=${#mystr}
echo $len

Reply

15 Merry Patel September 14, 2010 at 5:11 pm

I want program code for “Accept number and check the number is even or odd, finds the length of the number, sum of the digits in the number” in Unix.

Reply

16 Vishal June 24, 2011 at 6:06 am

Hi All,

I am looking for a script to compare the dates of two files to find out which one is newer.
Taking the following files as reference:

-rwxr-xr-x 1 vishy vishy 10088 Apr 26 11:08 file1
-rwxrwxr-x 1 vishy vishy 3232 Feb 12 2009 file2

We can see that for file1 the date and time(month date hrs:mins) is displayed, but for file2 month date and year is displayed.

Can anyone please share a script to compare the dates of files of these kind?

Regards,
Vishy

Reply

17 hoawa March 7, 2012 at 8:58 pm

what is the code for generating the number of char for A and B for unix and I want the input of the user to be prompted every time a input is done (e.g. loop)

So I would want te program to allow the user to repeatedly input a string of characters and produce the list of number of each character in the string and generate the percentage of each character in the string.

Any suggestions.

Reply

18 Preeti May 17, 2012 at 9:57 am

echo “enter 1se file name”
read f1
echo “enter 2nd fname”
read f2
if [ $f1 -nt -$f2 ]
then
echo ” $f1 is newer”
else
echo ” $f2 is newer”
fi

Reply

19 Tom March 28, 2013 at 8:19 pm

Just add this to your .bashrc:
——- cut ——-
strlen() {
str=”$@”
echo ${#str}
}
—— end cut ——
Then all you need is:
$ strlen This is some string
19
$ strlen “a”
1

;]

Reply

20 Peter W June 20, 2013 at 7:42 am

One previous suggestion was:
echo “${myVar}” | wc -c
However this includes the trailing character so the length is out by 1.

Using a generic expr command you can get the length on almost any platform or shell:
expr “${myVar}” : ‘.*’

Reply

21 Samapika Tripathy December 4, 2013 at 8:41 am

thank you

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , ,

Previous post:

Next post: