miércoles, 10 de diciembre de 2014

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
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

0 comentarios:

Publicar un comentario

Suscribirse a Enviar comentarios [Atom]

<< Inicio