Assigning a Value to a Variable

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

Command Environment icount=0 pdksh and bash set icount=0 tcsh


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

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

Command Environment myname=Sanjiv pdksh and bash set tcsh myname=Sanjiv

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="Sanjiv pdksh and bash


set myname="Sanjiv tcsh Guha"

Was this article helpful?

0 0

Post a comment