Cigarette Smoker---------Linux bash basic feature variables (3)

Source: Internet
Author: User
Tags terminates


Variable


program = instruction (read-only) + data (location variables, lists, arrays of data stores)

Directives can be called through positional variables, and data passed to the script


A variable name can refer to a "named Memory Space": The memory space of the name, which can be called by the name of the pre-divided memory space

Types of data stored internally by variable type variables

The role of variable types

Limit the storage space

The storage format is limited

Limit the operations that are involved


Types of variables

Character type

Numeric type

Integral type

Floating point Type


Types of variables

When a strongly typed variable is defined, the type of the variable must be specified, and the participating operations must conform to the requirements

Calling an unspecified variable will cause an error.


When you define a variable with a weak type , you do not specify the type of the variable, and the implicit conversion occurs automatically when participating in the Operation

Direct assignment, direct reference, default to character type


Var=value means store value value in var memory space


Bash weak type, interpreted type

1, do not declare, direct reference

2, participate in the operation, automatic conversion


Types of variables (by bash scope)

Local Variables the effective scope is the life cycle of the current shell , which is not valid for Shell processes other than the current shell and for the child or parent processes of the current shell .

environment variable effective scope The current shell process and its child shell processes are not valid for other shells. When you run the script, the script runs in the child shell of the current shell

The local variable takes effect in the current shell process and only takes effect on a piece of code (usually referred to)

Positional variables function: To script parameters, in the script script can be called to call the parameters of the script

Special variables generally refer to built-in variables, $? Return status, $ return state, instruction can be called through positional variables, parameters passed to script


Local variables

the effective range is the life cycle of the current shell , not valid for Shell processes other than the current shell and child or parent processes of the current shell .

1) variables defined in the current shell that are valid for the current shell (during the life of the current shell process)

[email protected] ~]# TTY #当前终端/dev/pts/2[[email protected] ~]# a=3 #在模拟终端2上定 A local variable [[email protected] ~]# echo $a #可以显示其变量值3


2) variables defined in the current shell that do not take effect in the new shell

[[email protected] ~]# TTY #另起一个shell, at this time/dev/pts/0[[email protected] ~]# echo $a on analog terminal 0 #显示其变量的值


3) Variables do not take effect in the shell process

[[email protected] ~]# pstree                      #显示当前系统上的进程树,systemd  --                                             #systemd为当前shell的父进程                    ├─sshd─┬─sshd───bash───pstree   #  location of current shell          [[email protected] ~]# bash                       # New launch of a sub-shell[[email protected] ~]# pstreesystemd  --          ├─sshd─┬─sshd───bash───bash───pstree  #当前shell为子shell [[email protected] ~]# echo  $a                      #变量不会生效  [[email protected] ~]#     [[email protected] ~]# exit                         #退出当前shell, back to Father shellexit[[email protected] ~]# pstreesystemd─        ├─sshd─┬─sshd───bash───pstree      #回到当前shell进程 [[ email protected] ~]# echo  $a                      #变量生效   3


Environment variables

Effective Scope The current shell process and its child shell processes are not valid for other shells

Comments

1) variables defined in the current shell, valid for the current shell and child shell

[[email protected] ~]# TTY #当前终端/dev/pts/2[[email protected] ~]# declare-x name=pidof #定义环境变量 [[ Email protected] ~]# echo $name pidof[[email protected] ~]# bash[[email protected] ~]# echo $namepidof [[Email Protected] ~]# Exitexit


2) variables defined in the current shell that do not take effect in the new shell

[[email protected] ~]# TTY #另起一个shell, at this time/dev/pts/0[[email protected] ~]# echo $name on analog terminal 0 [[Email PR Otected] ~]#


Positional variables

$1,$2,.... Invoke each parameter passed to the script

[Email protected] ~]# nano Psvar.sh#!/bin/bashecho "\$1:" echo "\$2: $" [[email protected] ~]# chmod +x psvar.sh [[Emai] L protected] ~]# ls-l psvar.sh-rwxr-xr-x 1 root root 1 15:10 psvar.sh[[email protected] ~]#./psvar.sh Tom jerry$ 1:tom$2:jerry


Special variables

$: Indicates the command itself, $? The execution status result of the command

# echo ' Echo ' \$0: $ "' >> Psvar.sh[[email protected] ~]#./psvar.sh Tom jerry$1:tom$2:jerry$0:./psvar.sh



Local variables

Assignment : stores The value represented in the name variable in the memory space

# name= ' value '

Name of memory space

= Assignment Symbol

Value values

"or" "reference symbol


Reference Implementation Assignment:

Plain Text reference: assigned a text literal to a variable

Variable reference: assigns the value of a variable to another variable

Command reference: assigns the execution result of the command to the variable:

Mixed use


Note: If a value has a space, it must be quoted when assigning a value


Assigning values to variable reference implementations

[Email protected] ~]# Username=pig[[email protected] ~]# echo $name [[email protected] ~]# name= $username [[Email protecte D] ~]# Echo $namepig

Command Reference Implementation Assignment

[[email protected] ~]# pwd/root[[email protected] ~]# name= ' pwd ' [[email protected] ~]# echo $name/root

Mixed use

# filename= ' Date +%f-%h-%m-%s ' # echo $filename 2017-08-01-15-41-45


Values have spaces and need to be quoted when assigning values

[[email protected] ~]# Name=tom Obama-bash:obama:command not found[[email protected] ~]# name= "Tom Obama" [Email protect ED] ~]# Echo $nametom Obama

variable reference , $name, ${name}, usually with $name. When a variable name is used in conjunction with a string that can be used as a variable name, so that the entire name is not treated as a variable, you must add {}

' Strong reference to the variable reference in which it is not replaced by the value stored in the variable

"" , the variable reference in which the weak reference is replaced by the value stored in the variable


1, when should use ${name}

# animal=pig# echo "There is some $animals" #把整个名字当作变量. Because this variable is undefined, it is empty there is some # echo "There is some ${animal}s" there is some pigs


2. The difference between strong and weak references

[[email protected] ~]# echo "$animal" variable will be replaced by pig [[email protected] ~]# echo ' $animal ' variable will not be replaced $animal


Show all local variables and environment variables: Set

Variable Destruction : When the shell terminates, the variable is destroyed. Destroying a variable before the shell terminates: unset name

[Email protected] ~]# Name=pika[[email protected] ~]# echo $namepika [[email protected] ~]# unset name[[email protected] ~ ]# echo $name [[email protected] ~]#


Environment variables

assignment values the value represented in the memory space of the name variable

Method One # Declare-x name= ' value ' method two # export Name= ' value ' method three # name=value# Declare-x name# export Name


References

# $name, ${name}


Show all environment variables

# export# env# printenv

Where uppercase is the environment variable: Define bash Work environment

Bash has many built-in environment variables: For uppercase variables, do not assign values directly, define environment variables, avoid bash's built-in environment variables, or change bash's work behavior.

Path,shell,uid,ps1,histsize,home,pwd,oldpwd

[Email protected] ~]# echo $PATH #bash解析命令所依赖的变量/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/ Usr/bin:/root/bin[[email protected] ~]# echo $SHELL #当前shell环境/bin/bash[[email protected] ~]# echo $UID #当前有效用户的 ID number 0[[email protected] ~]# echo $PS 1 #定义命令提示符字串 [\[email protected]\h \w]\$[[email protected] ~]# echo $HISTSIZE #显 1000[[email protected] ~]# echo $HOME #显示当前用户的家目录位置/root[[email protected] ~]# echo $PWD #显示当前所处的目录/root [ [Email protected] ~]# echo $OLDPWD #显示最近一次切换的目录 [[email protected] ~]#

variable destruction : unset

unset


The law of variable naming

1, can not use keywords and reserved words, if,for

2, can only use numbers, letters and underscores, can not start with a number

3, try to do, see the name of the meaning of understanding

Totalweight-Totalweight Avoid all caps--total_weight or with hyphens

4, try to avoid _ start

5. Try to avoid all caps


The law of file name naming

1. Case-sensitive

File1, File1, FILE1 are different files.

2. Do not use special characters

/: root directory, path delimiter

3, length less than 255 characters

4. Files that begin with. are hidden files


The philosophical thought of Linux

1. All Documents

2, combination of small programs

3. No interaction

4. Text editing configuration


constants, read-only variables : variables are not allowed to change after they are defined. Only wait until the shell process terminates, and then it dies.

Assign Value

Method one ReadOnly name method two declare-r name

Notes

[Email protected] ~]# Name=obama[[email protected] ~]# echo $nameobama [[email protected] ~]# readonly Name[[email Protect Ed] ~]# name=jerry-bash:name:readonly variable[[email protected] ~]# unset name-bash:unset:name:cannot unset:readonl Y variable


Local variables

Used in the function body

Positional variables

Directives can be called through positional variables, parameters passed to the script


Variable

$1,$2,... Corresponding call 1th 2nd 3rd ... Parameter The $ command itself

Call the arguments passed to the script

$* means that the passed arguments are treated as a string when called

[email protected] Each parameter as a separate string

$# the number of arguments passed to the script

[email protected] ~]# cat posvar.sh #!/bin/bashecho "\$1:" echo "\$2: $" echo "\$3: $" echo "\$*: $*" echo "\[email prot Ected]: [email protected] "echo" \$#: $# "echo" \$0: $ "
[Email protected] ~]#/posvar.sh Tom Jerry blair$1:tom$2:jerry$3:blair$*: Tom Jerry Blair[email protected]: Tom Jerry blair$#: 3$0:./posvar.sh


Change port operator shift [n] default kick one

[email protected] ~]# cat posvar.sh #!/bin/bashecho "\$1: $" Shiftecho "\$1:" Shift 2echo "\$1:" echo "\$*: $*" echo " \[email protected]: [email protected] "echo" \$#: $# "echo" \$0: $ "
[Email protected] ~]#/posvar.sh Tom Jerry Blair mariadb$1:tom$1:jerry$1:mariadb$*: Mariadb[email protected]: mariadb $#: 1$0:./posvar.sh


Pass parameters to the script and do the statistics for the lines of text

# Vim posvar.sh#!/bin/bashcountlines= ' cat $ | Wc-l ' echo ' has $countlines lines "
#./posvar.sh/etc/fstab/etc/fstab has ten lines





This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1952773

Cigarette Smoker---------Linux bash basic feature variables (3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.