Raspberry Pi_Eng_10.2.1 Variable


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


10.2   Shell Environment

 

10.2.1  Variable

 

10.2.1.1    What is Variable?

 

Variable is a place where data can be temporarily stored in the computer's memory. The difference with the file is that when the computer is shut down, the data in the file is retained when the computer is turned on again, and the data in the variable is lost when the computer is shut down.

If you use multiple words to define variable,, you can use "_" to connect them together.

 

RUN_MODE="daemons"

PIDDIR=/var/run/samba

 


 

10.2.1.2     Types of Variables

 

There are system variable and local variable according to the range to which they are applied. They have the following features:

 

   System variable

 

System variable is defined as a pre-reserved name at the Raspberry Pi system level and has the same variable name in any script, and can not be used for any other purpose in the script. Variables such as $ PATH are system variables.

 

These system variables are as follows:

 

    Environment variable  

 

variable name

description

BASH

Shell name

BASH_VERSION

Shell version

COLUMNS

column

LINES

line

HOME

home directory

LOGNAME

login name

OSTYPE

OS type

PATH

path setting value

PS1

prompt setting value

PWD

working directory

SHELL

Shell name

USERNAME

current login user id user ID

Table 10‑1 Environment variable

    Other special System variable

 

variable name

description

$$

PID value of the corresponding process

$?

return value of the most recently executed command

$#

Number of arguments passed to the program

$0

Variable value to remember command name when executing the command

$n

The n-th passed argument (ex: $1, $2)
The parameters $1, $2 ...are variables that are assigned in order when the command is executed.

$*

Marks all passed arguments as a single string

$@

List all passed arguments as a list of strings

$!

PID value of the last executed background process

Table 10‑2 Special System variable

 

 

   Local variable

 

On the other hand, the local variable is the variable that is used only when the script is executed. As the variable itself disappears and the value is also lost when the execution of the corresponding script is terminated, the local variable is restarted with the new value when the script is executed next time. Variables of the same name can be used for other purposes in other scripts,, and there is no correlation between them.

 


 

10.2.1.3    Declaring Data Types of Variable

 

In the Shell, variables are not defined before use. Values stored in variables are treated as strings by default and are case-sensitive. Numbers alos are basically treated as strings, but calculations are allowed for arithmetic operations in the same way as ordinary numbers.

If you want to restrict the use of data type for a variable, you may use it after explicitly defining it with "declare" or "typeset" statements.

 

[Command Format]

declare/ typeset   <변수>  <Type>

 

[Command Overview]

   This specifies the data type that can be used for a variable.

   User privilege          -- Normal user.

 

[Detail Description]

   The following data types can be specified:

    -i         -- Integer

    -a        -- Array

    -f         -- Function

    -r        -- Read only

    -x        -- Available variable outside the Shell

10.2.1.4    "unset" Command - Delete Variable Definition

 

The "unset" command is to remove previously defined variables and make them undefined.

 

[Command Format]

unset  <variable>

 

[Command Overview]

   This deletes the previously defined variable.

   User privilege          -- Normal user.

 

[Detail Description]

   After unset, the variable does not exist.

 

[Used Example]

 

pi@raspberrypi ~ $ var="1234"

pi@raspberrypi ~ $ echo $var

1234

pi@raspberrypi ~ $ unset var

pi@raspberrypi ~ $ echo $var

-bash: var: unbound variable

 


 

10.2.1.5    "set" Command - Set Shell Call Parameter


When you call the Shell script file, you can pass parameters. When you refer to this parameter in the script, You can refer to it as a variable named $1, $2, .... The "set" command sets the value of this parameter given when the script is called.

[Command Format]

set  value1  value2  ..

 

[Command Overview]

   This sets the value of the parameter to be passed when the script is called.

   User privilege          -- Normal user.

 

[Detail Description]

   The value to be set is assigned to $1, $2, $ 3 ... in order.

 

[Used Example]

 

pi@raspberrypi ~ $ set "This is 1" "This is 2"

pi@raspberrypi ~ $ echo $1

This is 1

pi@raspberrypi ~ $ echo $2

This is 2