|
While writing a shell script you might want to find 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 "abusittik" word:
expr length "abusittik"
Output:
9
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="abusittik"
echo ${#myVar}
Output:
9
Using AWK :
% echo abusittik | awk ‘ { print length } ‘
Output : 9
Using Perl:
% echo abusittik | perl -nle ‘ print length ‘
Output : 9
Great blog, thanks for taking time to share this page. It is really helpful. I have learned how to find the length of a string in Linux/Unix Shell scripting. Keep up the good work and share more.
ReplyDeleteUnix Training institutes in Chennai | Unix certification in Chennai
Great content. Well written and well executed. I'm glad that I came across your post. Keep posting. Regards.
ReplyDeleteUnix Training in Chennai | Unix Shell Scripting Training in Chennai | Unix Course in Chennai