String length: Difference between revisions

Content added Content deleted
(→‎Joy: add)
m (→‎{{header|UNIX Shell}}: Add note about bytes vs characters in implementations)
Line 3,612: Line 3,612:


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
===Byte Length===
====Byte length via external utility:====
====With external utility:====


{{works with|Bourne Shell}}
{{works with|Bourne Shell}}
Line 3,621: Line 3,620:


====With [[Unix|SUSv3]] parameter expansion modifier:====
====With [[Unix|SUSv3]] parameter expansion modifier:====

This returns the byte count in ash/dash, but the character count in bash, ksh, and zsh:


{{works with|Almquist SHell}}
{{works with|Almquist SHell}}
{{works with|Bourne Again SHell|3.2}}
{{works with|Bourne Again SHell}}
{{works with|pdksh|5.2.14 99/07/13.2}}
{{works with|Korn Shell|93}}
{{works with|Z SHell}}
{{works with|Z SHell}}
<syntaxhighlight lang="bash">string='Hello, world!'
<syntaxhighlight lang="bash">string='Hello, world!'
length="${#string}"
length=${#string}
echo $length # if you want it printed to the terminal</syntaxhighlight>
echo $length # if you want it printed to the terminal</syntaxhighlight>