Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet17/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   13   14   15   16   17   18   19   20   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

#
where 
#
is the number as shown in the history list. For 
instance, to repeat the vi command from Figure 2.3, enter 
!9
.
1 ls
2 cd ~
3 pwd
4 man cd
5 ls –l
6 cd /bin
7 ls –l | grep root
8 cd ~
9 vi script1
10 cat script1
11 rm script1
12 history
FIGURE 2.3 
Sample History.


The Bash Shell

47
There are several different ways to recall commands from the history list using the ! 
(sometimes called a “bang”). These are:
• 
!
#
—Recall the instruction from line 
#
in the history list
• 
!!
—Recall the last instruction from the history list (in the case of Figure 2.3, it would 
re-execute history)
• 
!
string
—Where 
string
is a string of character to recall the most recent instruction 
that started with the given string
If you typed 
!c
, the cat command would execute whereas if you entered 
!cd
, the 
cd ~
command (number 8) would execute. Notice that !c would yield an error message because 
command #11 deleted the file and so using cat to display the file would not work. We 
explore cat, rm, cd, and other commands listed above in Chapter 3.
Another way to recall instructions is to step through the history list from the command 
line. This can be done by either pressing the up arrow key or typing cntrl
+
p (i.e., press-
ing the control key and while holding it down, typing p). By repeatedly 
pressing the 
up arrow key or cntrl
+
p, you are moving instruction by instruction through the his-
tory list from the most recent instruction backward. When you reach the instruction 
you want, press the enter key. Using cntrl
+
p or the up arrow key is just one form of 
command line editing. You can also step your way forward through the history list 
once you have moved back some number of commands by using cntrl
+
n or the down 
arrow key.
As with ls, history has a number of options available. The command history by itself lists 
the entire history list up to a certain limit (see Section 2.5). Useful options are as follows.
• -c—Clear the history list
• -d #—Where # is a number, delete that numbered entry from the history list
• -w—Write the current history list to the history file
• -r—Input the history file and use it instead of the current history
• #—Where # is a number, display only the last # items in the list
2.4.2 Shell Variables
A shell is not just an environment to submit commands. As with the history list, the shell 
also remembers other things, primarily variables and aliases. Variables and aliases can be 
established in files and input to the interpreter or entered via the command line.
A variable is merely a name that references something stored in memory. Variables are 
regularly used in nearly every programming language. Variables, aside from referencing 
memory, have associated with them a type. The type dictates what can be stored in that 
memory location and how that memory location should be treated.


48

Linux with Operating System Concepts
In many programming languages, variables must be declared before they can be used. 
This is not the case in Bash. Instead, when you want to first use a variable, you assign it a 
value. The assignment statement takes the form
var
=
value
where 
var
is a variable name and 
value
is a the initial value stored in the variable. Spaces 
around the equal sign are not permissible. The variable name can consist of letters, digits, 
and underscores (_) as long as it begins with a letter or underscore, and can include names 
that had already been used. However, it is not recommended that you use names of envi-
ronment variables as the environment variables are set up for specific uses and overriding 
their values can be dangerous.
A variable in Bash is 
typeless
. This means that you do not have to state what type of item 
will be stored in a variable, and the type can change over time. The only types available in 
Bash are strings, integers, and arrays. If the number contains a decimal point, the number 
is treated as a string. Only integers can be used in arithmetic operations. In fact, all vari-
ables store strings only but as we will learn in Chapter 7, we can force Bash to treat a string 
as an integer when performing arithmetic operations.
Once a variable has a value, it can be used in other assignment statements, output state-
ments, selection statements, and iteration statements. We will save the discussion of selec-
tion and iteration statements until Chapter 7. To retrieve a value stored in a variable pre-
cede the variable name with a $ as in $X or $NAME.
The command to output the value of a variable is 
echo
. This is the general purpose out-
put statement for Linux. The format is
echo 
string
where 
string
can be any combination of literal characters and variable names. The variable 
names must be preceded by a $ in order to output the values stored in the variables. For 
instance, assume 
name 
=
Richard
. Then
echo name
outputs
name
whereas
echo $name
outputs
Richard


The Bash Shell

49
The echo command has a few basic options:
• -n—Do not output a newline character at the end. If you use –n, then the next echo 
statement will output to the same line as this echo statement. By default, echo outputs 
a newline character at the end of the output.
• -e—Enable interpretation of backslash escapes (explained below).
• -E—Disable interpretation of backslash escapes.
The echo statement accepts virtually anything as a string to be output. Some examples 
are shown below. Assume that we have variables first and last which are storing Frank and 
Zappa, respectively.
echo $first $last
Frank Zappa
echo hello $first $last
hello Frank Zappa
echo hello first last
hello first last
echo $hello $first $last
Frank Zappa
Notice in the last case, the variable hello has no value. In Linux, we refer to this as the 
value null. Trying to output its contents using $hello results in no output rather than an 
error.
If the string is to include a character that has a special meaning, such as the $, you must 
force the Bash interpreter to treat the character literally. You do this by preceding the char-
acter with a \ (backslash). For instance, if 
amount
stores 183 and you want to output this 
as a dollar amount, you would use
echo \$ $amount
which would output
$ 183
The space between \$ and $ was inserted for readability. The instruction 
echo 
\$$amount
would output
$183
Other escape characters are shown in Table 2.3.
It is not very common to create your own variables from the command line prompt. 
However, there are multiple variables already defined that you might find useful. These are 


50

Linux with Operating System Concepts
known as environment variables. To see the environment variables defined, use the com-
mand 
env
. Some of the more useful variables defined in the Bash environment are shown 
in Table 2.4.
The PS1 variable can be altered by the user which will then alter the appearance of the 
user’s prompt. If you output PS1 using echo, you might find its contents to be particularly 
cryptic. For instance, it might appear as [\u@\h \W]$. Each character stored in the prompt 
variable is output literally unless the character is preceded by a \, in which case it is treated as 
an escape character. But unlike the escape characters of Table 2.3, these characters inform the 
Bash interpreter to fill them in with special information from various environment variables.
Table 2.5 indicates the more useful escape characters available for PS1. For instance, 
we might modify PS1 to store 
[\t - \!]$
which would output as a prompt 
[
time - 
number
]$
where 
time
is the current time and 
number
is the history list number as in 
[11:49 - 162]$
.
There are also options for changing the color of the prompt and formatting the time and 
date.
TABLE 2.3 
Escape Characters for Linux echo

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   13   14   15   16   17   18   19   20   ...   254




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish