Page 127 - DCAP106_OPERATING_SYSTEM_TOOLS
P. 127
Operating System Tools
Notes The following example output shows that we are using bash shell:
PID TTY TIME CMD
13931 pts/4 00:00:00 bash
7.1.1 Shell Variables
A shell variable is a means of citing a character or numeric value. And unlike formal programming
languages, a shell script doesn’t require you to declare a type for your variables. Thus, you could
assign a number to the variable
stuff
and then make use of it again in the same script in order to hold a string of characters.
Example: To access the value (contents) of a variable, prefix it with a dollar sign.
stuff=5
stuff=’chocolate truffles’
Don’t put any spaces before or after the equal sign, or it will produce an error. To assign a string
including spaces, it is required to put quotation marks around the string.
Notes This is to note that there are several distinct ways to use quotations marks in a shell
script.
Now let us discuss some differences between single quotation marks, double quotation marks,
and the backslash character:
Single quotation marks will always get you exactly what’s inside the quotation marks. Any
characters that might otherwise have special meaning to the shell (like the dollar sign or the
backslash) are treated literally.
Use double quotation marks when you want to assign a string including special characters the
shell should act on.
We use backslash to escape a single character (such as $ or *) that might otherwise be considered
as a special character by the shell.
Now we will discuss some examples that illustrate when to use each method of quoting.
Example:
howdy=’Good Morning $USER !’
echo $howdy
Good Morning $USER !
howdy=”Good Morning $USER !”
echo $howdy
Good Morning hermie !
In the first case, the howdy variable value would perhaps not be what you required. The single
quotation marks caused Bash to not treat $USER as a variable. In the second case, the results look
120 LOVELY PROFESSIONAL UNIVERSITY