Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is there any function in Ksh/Sh to make a string length to fixed length, for example, 5 characters length, and add space from left to the strings if it's length not up to 5?
Thanks,

The function you are looking for is "typeset"
Option Operation
-Ln Left-justify. Remove leading blanks; if n is given, fill with blanks or truncate on right to length n.
-Rn Right-justify. Remove trailing blanks; if n is given, fill with blanks or truncate on left to length n.
-Zn Same as above, except add leading 0's instead of blanks if needed.
-l Convert letters to lowercase.
-u Convert letters to uppercase.Here are a few simple examples. Assume that the variable alpha is assigned the letters of the alphabet, in alternating case, surrounded by three blanks on each side:
alpha=" aBcDeFgHiJkLmNoPqRsTuVwXyZ "
Examples of typeset String Formatting Options Statement Value of v
typeset -L v=$alpha "aBcDeFgHiJkLmNoPqRsTuVwXyZ "
typeset -L10 v=$alpha "aBcDeFgHiJ"
typeset -R v=$alpha " aBcDeFgHiJkLmNoPqRsTuVwXyZ"
typeset -R16 v=$alpha "kLmNoPqRsTuVwXyZ"
typeset -l v=$alpha " abcdefghijklmnopqrstuvwxyz"
typeset -uR5 v=$alpha "VWXYZ"
typeset -Z8 v="123.50" "00123.50"

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |