Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

do not have to specify whether a variable is a number or a string, and so on.


Assigning a Value to a Variable


Suppose that you want to use a variable called lcount to count the number
of iterations in a loop within a shell program. You can declare and initialize
this variable as follows:


Command Environment

lcount=0 pdksh  and bash

set lcount=0tcsh

NOTE
Under pdksh and bash, you must ensure that the equals sign (=) does not
have spaces before and after it.

To store a string in a variable, you can use the following:


Command Environment

myname=Sedona pdksh and bash

set myname=Sedonatcsh

Use the preceding variable form if the string doesn’t have embedded spaces.
If a string has embedded spaces, you can do the assignment as follows:


Command Environment

myname=“Sage” pdksh and bash

set myname=“Sage”tcsh

Accessing Variable Values


You can access the value of a variable by prefixing the variable name with a
dollar sign ($). That is, if the variable name is var, you can access the
variable by using $var.

Free download pdf