LiveFire Labs - Online UNIX Training - Company Logo


Online UNIX Training with Hands-on Internet Lab


"Eliminate the expense and inconvenience of classroom training without eliminating the classroom experience."

 


Home
Internet Lab
Console Access
Sample Course

Student Login


LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week - View


Questions? Call
1.888.843.1637 or send us email





December 22, 2003 - Managing Variable Attributes with typeset - Part II

In addition to controlling the case (lower/upper) of a variable, it is often useful to specify the justification and also the width if desired.  These attributes are set by using the -L and -R options.  The width may optionally be defined by adding a numeric value immediately after the -L or -R.

In this first example, the variable MY_VAR1 is defined to be left justified and has a maximum length of 5 characters:

$ typeset -R5 MY_VAR1
$ BORDER="|"
$ MY_VAR1="owl"
$ print "${BORDER}${MY_VAR1}${BORDER}"
|  owl|
$

A variable named BORDER was used to show you that the shell will pad unused character positions with spaces.  In the next example, notice how the shell truncates the variable to maintain the pre-defined width of 5:

$ typeset -L5 MY_VAR2
$ BORDER="|"
$ MY_VAR2="falcon"
$ print "${BORDER}${MY_VAR2}${BORDER}"
|falco|
$

If a width is not specified for a variable, the shell defines it to be the length of the first value assigned to it:

$ typeset -L MY_VAR3
$ BORDER="|"
$ MY_VAR3="raptor"
$ print "${BORDER}${MY_VAR3}${BORDER}"
|raptor|
$ MY_VAR3="eagle"
$ print "${BORDER}${MY_VAR3}${BORDER}"
|eagle |
$




Learn more...
  

If you are new to the UNIX or Linux operating system and would like to learn more, you may want to consider registering for LiveFire Labs' UNIX and Linux Operating System Fundamentals online training course.

If you already have a solid grasp of the fundamentals but would like to learn more about the Korn shell and basic and advanced shell scripting, taking our Korn Shell Scripting course will be beneficial to you.

Our innovative hands-on training model allows you to learn UNIX by completing hands-on exercises on real servers in our Internet Lab.


More Tips...

· 
Popular UNIX Tips from the Past

spacer Box Border
 

Receive the UNIX Tip, Trick, or Shell Script of the Week by Email


First Name:


Email Address:






   1.888.843.1637

Home - Contact us - Company info - Privacy Statement   

 
©2002-2003 LiveFire Labs.  All rights reserved.
Linux® is a registered trademark of Linus Torvalds, author and developer of this public domain operating system.
UNIX® is a registered trademark of The Open Group in the United States and other countries.