
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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 17 comments… read them below or add one }
Few more examples:
% echo nixcraft | awk ‘ { print length } ‘
8
% echo nixcraft | perl -nle ‘ print length ‘
Corey,
Nice awk / perl tips :)
Appreciate your post!
echo -n “hello world” | wc -c
Thanks for the tips
It was very useful for my work
Greetings from Mexico
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
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.
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”.
use length=`expr length $string` it will work
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
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
Hey !
Thanks for the tip ! resolved my issue !
Expr lenght was working under linux but not under Solaris.
myVar=”nixcraft”
echo ${#myVar}
echo ${myVar} | awk ‘ { print length } ‘
echo “${myVar}” | wc -c
echo `expr length ${myVar}`
Another simple way is:
mystr=”A random string”
len=${#mystr}
echo $len
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.
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
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.
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