A free Basic UNIX-training with muLinux
Notes: This Text is roughly translated (German to English) by a translating program. Sorry, but I have not time for the translation. Maybe you can do this?
Shell Variables
What is a Shell?
Shell is the command interpreter under Unix. That sounds only times complicated.
We however already operated the whole time with the Shell. We have instruction,
like z. B. LS or DATE , input and us about the reactions of
Unix pleases. This dialog is implemented by the command interpreter. This is
situated like a bowl (English Shell) over the operation system and provides for
communication with the user.
Strictly speaking there are several Shells (C, grain, under Unix Bash...).
Each user can decide for its favourite shell muLinux used however only a Shell.
The differences of the Shells are small (for normal users) and should not not be
interested therefore for the time being.
What is a variable?
A variable is only a name, to which a value was assigned. There are three
variable types:
- System variables (vordefenierte variables)
- Special variables
- User-defined variables
System variables determine the characteristics system the user opposite and
can by the user be adapted. System variables are written with uppercase letters.
The special variables can be only accessed reading. They determine general
system properties.
User-defined variables can be produced, modified and deleted by the user.
They can be used e.g. for shell Skripte. Those are small programs, which can
simplify themselves repeating work.
Displays of available variables
For the display the instruction serves all variable set .
/# set
The system variables with contents are displayed. Left are the variable
names, then an equals sign and to it contents of the variables come. In order to
see all variables, we must the sceen contents scrollen ( [ SHIFTS ] [
picture on ] , [ SHIFTS ] [ picture off ] ). for it there is
nevertheless very many, as the following instruction displays us:
/# set | wc
We want to discuss only some system variables here. At the beginning we look
ourselves times the system variable PS1 on. Who does not want to
scrollen the display, can also grep leave only the line with PS1 to
display:
/# set | grep "PS1"
PS1=/#
/#
The variable PS1 contained thus the two characters / # .
What is that? That comes us nevertheless somehow admits forwards. Yes, that is
our prompt! Times see whether we these to modify being able.
Assign contents
A variable is defined with the following syntax, whereby no blanks before or after the equals sign may be:
Variable=value
We want to modify the prompt in such a way that " instruction? " as prompt appears:
/# PS1=Command?
Command?
More beautiful would it however be with a blank after "Command?".
If we want to have blanks, we must include the value in stating lines:
/# PS1="Command? "
Command?
This is also necessary, if we want to assign several words as value:
/# PS1="Please type a command: "
Please type a command:
Who wants can also show the DOS-PROMPT PS1="C:\> "
Display to a variable
For displaying a variable the instruction can echo are used.
echo text outputs. This instruction is necessary with shell Skripts.
Please type a command: echo PS1
PS1
Please type a command:
Thus unfortunately only the text "PS1" is displayed and not contents of PS1.
In addition dollar character must $ before the variable are:
Please type a command: echo $PS1
Please type a command:
Please type a command:
We can mix also text and variable:
Please type a command: echo "That is the content of PS1: $PS1"
That is the content of PS1: Please type a command:
Please type a command:
Make variables empty
In order to empty a variable, one inputs to the variable names only followed
from an equals sign:
/# PS1=
The variable remains existing thereby however. It is only empty.
set | grep "PS1"
PS1=
If we PS1, have we empty no more prompt. What actually occurs when changing into another directory?
cd /home
/home#
The variable PS1 is thus overwritten here by the system.
Write protection for variables
With the instruction readonly variables can be protected from a possible modification.
/# readonly PS1
Each attempt PS1 to modify, is acknowledged with an error message.
The write protection is preserved up to leaving the Shell.
One calls the instruction readonly without parameters up, all write protected variables are displayed.
/# readonly
PS1
User-defined variables
In order to define own variables, the following must be considered with the
designation:
- Variable names knows from small or uppercase letters, which underlined digits or exist. Umlauts (ae, oe, ue, ss) may not be used.
- A digit and underlined may not as the first character be.
- The length of the variable name is unlimited.
- It is recommended to write variable names only small. So they are to be differentiated more easily from the system variables.
Variable ones can be used for simplification or in repetition of instruction.
So for example as instruction option:
/# a=/usr/bin
/# cd $a
/usr/bin #
Or as instruction:
/# b="ls -l"
/# $b
long output de Verzeichnisinhaltes
We can store also the outputs of programs into a variable.
In addition we must the program call in two rueckwertige inverted comma (franz. Accent grave) ` put in parentheses.
/# today="Today is `date`"
/# echo $heute
Today is November 16 15:54:16 1999
/#
Extend the variables' content
/# gosh=Linux
/# gosh=mu$toll
/# echo $gosh
muLinux
/#
Delete from variables
For the deletion variable serves the instruction unset :
/# unset a
/#
Robert.Warnke@giso.de (copyleft) Robert Warnke, Berlin (Germany) - You can write me in English. | http://rowa.giso.de
|