Variables in the Shell (definition methods of three different variables, translation of variables, functions in scripts and parameter transfer)

1. Definition of variables:

1)定义本身
变量就是内存一片区域的地址

2)变量存在的意义
命令无法操作一直变化的目标
用一串固定的字符来表示不固定的目标可以解决此问题

2. Three kinds of variable definition methods in shell script:
Insert picture description here

(1) Environment variable: haha=22 is a variable set in the current shell environment, sh w.sh opens a new child process, sh w.sh has no value
Insert picture description here
export means that the child process shares the resources in the current shell (set Environment variables):
Insert picture description here

(2) Setting of user variables: Note: After editing the file, remember to source ~/.bash_profile to make the settings in the file take effect immediately:
Insert picture description here

(3) Setting of system variables: it will still take effect after exiting the shell. You can view the set system variables through env:
Insert picture description hereInsert picture description here

3. The name of the variable:

"字符" "_" "数字" 
不能用数字开头
建议:
变量名称短全用大写字符
变量名称长用_区分子类
WESTOS
Westos_Linux
westoS_Linux

4. Translation of variables:
Insert picture description here
Insert picture description here

5. Alias ​​setting of user command:

alias 查看系统中的命令别名

alias xie='vim'		##临时设定

vim ~/.bashrc
alias xie='vim'		##只针对与用户生效

vim /etc/bashrc		##针对系统所以用户生效
alias xie='vim'

unalias xie		##删除当前环境中的alias

6. Passing parameters in the script:

非交互模式:
$0 is /mnt/test.sh		       ##脚本本身
$1 is westos			       ##脚本后所输入的第一串字符
$2 is linux                    ##脚本后所输入的第二串字符
$3 is redhat                   
$* is westos linux redhat	   ##脚本后所输入的所有字符"westos linux redhat"
$@ is westos linux redhat	   ##脚本后所输入的所有字符'westos' 'linux' 'redhat'
$# is 3				           ##脚本后所输入的字符串个数

交互模式传参:
read  WESTOS			                        ##对westos赋值
read -p "please input word:"  WESTOS	        ##输出提示语,将输入的值赋给WESTOS
-s                                              ##无回显输入	

Note: The difference between $* and $@

7. Functions in the script:

程序的别名

设定方式:
WORD()
{
	action1
	action2
}

WORD 在脚本中就代表action1 action2这两个动作

Guess you like

Origin blog.csdn.net/lb1331/article/details/111592237