|
|||
Department of Engineering | |
University of Cambridge > Engineering Department > computing help |
A shell variable can be given a type (just as variables in Java or Fortran can), but this isn't necessary. Typing
pix=/usr/lib/X11/bitmaps(note: no spaces around the `=' sign) creates a variable `pix' whose value can be accessed by preceding the name with a `$'. `
cd $pix
'
will take you to a directory of icons. If you type
pix=8then pix will be treated as a number if the context is appropriate. The let command built into the shell performs integer arithmetic operations. It understands all the C operators except for and . The typeset command can be used to force a variable to be an integer, and can also be used to select the base used to display the integer, so
pix=8 let "pix=pix<<2" echo $pix typeset -i2 pix echo $pixwill print 32 then
2#100000
onscreen. The let
command (note that $
signs aren't necessary before the operands) uses
C's <<
operator to shift bits 2 to the left and the
typeset command makes pix into a base 2 integer.
typeset can do much else too. Look up the shell man page for a full list. Here are just a few examples
# You can control the how many columns a number occupies and whether # the number is right or left justified. The following # typeset command right-justifies j in a field of width 7. j=1 typeset -R7 j echo "($j)" # Variables can be made read-only. This j=2 assignment causes an error. typeset -r j j=2 # String variables can be made upper or lower case. string=FOO echo $string typeset -l string echo $string