OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Assigning Values To Variables And Constants

Here is where our knowledge of expansion really starts to pay off. As we have seen, vari- ables are assigned values this way:

variable=value

where variable is the name of the variable and value is a string. Unlike some other pro- gramming languages, the shell does not care about the type of data assigned to a variable; it treats them all as strings. You can force the shell to restrict the assignment to integers by using the declare command with the -i option, but, like setting variables as read- only, this is rarely done.

Note that in an assignment, there must be no spaces between the variable name, the equals sign, and the value. So what can the value consist of? Anything that we can ex- pand into a string:


a=z

b="a string"

c="a string and $b"


d=$(ls -l foo.txt) e=$((5 * 7)) f="\t\ta string\n"

# Assign the string "z" to variable a.

# Embedded spaces must be within quotes.

# Other expansions such as variables can be

# expanded into the assignment.

# Results of a command.

# Arithmetic expansion.

# Escape sequences such as tabs and newlines.

a=z

b="a string"

c="a string and $b"


d=$(ls -l foo.txt) e=$((5 * 7)) f="\t\ta string\n"


image

Multiple variable assignments may be done on a single line:



a=5 b="a string"

a=5 b="a string"


During expansion, variable names may be surrounded by optional curly braces “{}”. This is useful in cases where a variable name becomes ambiguous due to its surrounding con-


text. Here, we try to change the name of a file from myfile to myfile1, using a vari- able:



[me@linuxbox ~]$ filename="myfile" [me@linuxbox ~]$ touch $filename [me@linuxbox ~]$ mv $filename $filename1

mv: missing destination file operand after `myfile' Try `mv --help' for more information.

[me@linuxbox ~]$ filename="myfile" [me@linuxbox ~]$ touch $filename [me@linuxbox ~]$ mv $filename $filename1

mv: missing destination file operand after `myfile' Try `mv --help' for more information.


This attempt fails because the shell interprets the second argument of the mv command as a new (and empty) variable. The problem can be overcome this way:



[me@linuxbox ~]$ mv $filename ${filename}1

[me@linuxbox ~]$ mv $filename ${filename}1


By adding the surrounding braces, the shell no longer interprets the trailing 1 as part of the variable name.

We’ll take this opportunity to add some data to our report, namely the date and time the report was created and the username of the creator:



#!/bin/bash

# Program to output a system information page TITLE="System Information Report For $HOSTNAME"

CURRENT_TIME=$(date +"%x %r %Z")

TIMESTAMP="Generated $CURRENT_TIME, by $USER"


echo "<HTML>

<HEAD>

<TITLE>$TITLE</TITLE>

</HEAD>

<BODY>

<H1>$TITLE</H1>

<P>$TIMESTAMP</P>

</BODY>

</HTML>"

#!/bin/bash

# Program to output a system information page TITLE="System Information Report For $HOSTNAME"

CURRENT_TIME=$(date +"%x %r %Z")

TIMESTAMP="Generated $CURRENT_TIME, by $USER"


echo "<HTML>

<HEAD>

<TITLE>$TITLE</TITLE>

</HEAD>

<BODY>

<H1>$TITLE</H1>

<P>$TIMESTAMP</P>

</BODY>

</HTML>"


Top OS Cloud Computing at OnWorks: