Shell scripting tip: Find the length of a string under UNIX / Linux oses

by Vivek Gite on August 29, 2007 · 15 comments

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:
expr length "nixcraft"
Output:
8

expr and POSIX

However expr command is not concerned with POSIX (open system standards based on Unix). You can try old good KSH/Bash command (also following command should work with other UNIX like oses such as FreeBSD / Solaris ):
myVar="nixcraft"
echo ${#myVar}

Output:
8

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 15 comments… read them below or add one }

1 Corey Hart August 29, 2007

Few more examples:

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

% echo nixcraft | perl -nle ‘ print length ‘

Reply

2 vivek August 29, 2007

Corey,

Nice awk / perl tips :)

Appreciate your post!

Reply

3 name December 17, 2008

echo -n “hello world” | wc -c

Reply

4 crazysenses April 29, 2009

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

Reply

5 hw May 10, 2009

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

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

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

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

Reply

9 Will July 3, 2009

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

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

Hey !

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

Reply

12 Patwari June 22, 2010

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

Reply

13 alhaizan July 6, 2010

Another simple way is:

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

Reply

14 Merry Patel September 14, 2010

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

15 Vishal June 24, 2011

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

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 13 + 5 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: