当前位置:网站首页>Shell variables, system predefined variables $home, $pwd, $shell, $user, custom variables, special variables $n, $, $*, [email protected],
Shell variables, system predefined variables $home, $pwd, $shell, $user, custom variables, special variables $n, $, $*, [email protected],
2022-07-23 08:26:00 【Redamancy06】
List of articles
- 1. Variable
- 1.1 System predefined variables
- 1.2 Custom variable
- 1.2.1 Basic grammar
- 1.2.2 Variable definition rules
- 1.2.3 Case study
- 1.2.3.1 Defining variables a
- 1.2.3.2 View undefined sssshell
- 1.2.3.3“=” There are spaces before and after the sign
- 1.2.3.4 Change the value of the variable
- 1.2.3.5 When there are spaces in the content to be assigned, you need to add single quotation marks or double quotation marks
- 1.2.3.6 Local and global variables
- 1.2.3.7 take ssshell Define as global variable
- 1.2.3.8 In the child shell Change parent in shell Global variables are only in children shell It works , Back to the father shell It won't change
- 1.2.3.9 use vim Output the difference between global variables and local variables
- 1.2.3.10 stay set Look at custom variables
- 1.2.3.11 Declare static variables
- 1.2.3.12 Revoke variables a
- 1.2.3.13 Static variables cannot be undone
- 1.2.3.14 stay bash in , Variable default type is string type , No direct numerical operation
- 1.2.3.15 If you need to use , Need to use shell Expression of operators in
- 1.3 Special variables
1. Variable
1.1 System predefined variables
1.1.1 Common system variables
$HOME、$PWD、$SHELL、$USER etc.

1.1.2 Look at all the global variable values
[[email protected] scripts]# env
If you want to see it conveniently, you can add | less

[[email protected] scripts]# printenv
[[email protected] scripts]# printenv USER
There is no need to add $ Symbol , He can print all or part shell Environmental Science
[[email protected] scripts]# set

Look at all variables, including global variables and custom variables, local variables
1.2 Custom variable
1.2.1 Basic grammar
1) Defining variables : Variable name = A variable's value , Be careful ,= There must be no spaces before or after the sign
2) Revoke variables :unset Variable name
3) Declare static variables :readonly Variable , Be careful : You can't unset
1.2.2 Variable definition rules
1) Variable names can be alphabetized 、 Numbers and underscores , But it can't start with a number , Environment variable name is recommended to be capitalized .
2) No spaces on both sides of equal sign
3) stay bash in , Variable default type is string type , No direct numerical operation .
4) Value of variable if there is a space , Double or single quotes are required .
1.2.3 Case study
1.2.3.1 Defining variables a
[[email protected] scripts]# a=2

1.2.3.2 View undefined sssshell
[[email protected] scripts]# echo $sssshell
1.2.3.3“=” There are spaces before and after the sign

1.2.3.4 Change the value of the variable

1.2.3.5 When there are spaces in the content to be assigned, you need to add single quotation marks or double quotation marks

1.2.3.6 Local and global variables
env You can only check the global variables set by the system , and set You can view all defined variables , At this stage, I can't completely know ssshell Is it a local variable or a global variable? Go on 
Open a sub shell
explain ssshell It's a local variable
1.2.3.7 take ssshell Define as global variable
When we need to be in the son shell Use in ssshell variable , We need to ssshell Define as global variable , Definition ssshell When it becomes a global variable, you need to create ssshell The father of shell change , Not in other places shell change 

1.2.3.8 In the child shell Change parent in shell Global variables are only in children shell It works , Back to the father shell It won't change

1.2.3.9 use vim Output the difference between global variables and local variables

stay hello.sh Add local variables to sssshell And then execute hello.sh As the result of the 
If the absolute path is used, there is no hello,linux This one is , because sssshell It's a local variable
hold sssshell When the variable becomes a global variable, the result is 
Both show
1.2.3.10 stay set Look at custom variables
Defined a Variable , stay set You can find


1.2.3.11 Declare static variables
[[email protected] ~]# readonly b=2

Both static and local variables are set Inside
1.2.3.12 Revoke variables a
[[email protected] ~]# unset a


set There's no a The variable
1.2.3.13 Static variables cannot be undone

1.2.3.14 stay bash in , Variable default type is string type , No direct numerical operation

1.2.3.15 If you need to use , Need to use shell Expression of operators in
[[email protected] ~]# a=$((1+4))

[[email protected] ~]# a=$[5+2]

1.3 Special variables
1.3.1$n
1.3.1.1 Basic grammar
$n ( Function description :n Is the number ,$0 Represents the script name ,$1-$9 Represents the first to ninth parameters ,
More than ten parameters , More than ten parameters need to be enclosed in braces , Such as ${10})
1.3.1.2 Case practice
1.3.1.2.1 stay vim Inside "" and ’' The difference between
Enclose with double quotation marks $n And enclose with single quotation marks $n
Double quotation mark expanding $n Code variables
No matter what single quotation marks surround, the output is intact
1.3.1.2.2$n Use



If you use the absolute path, you need to give parameter.sh Plus Execution Authority 
1.3.2$#
1.3.2.1 Basic grammar
$# ( Function description : Get the number of all input parameters ,
Commonly used in cycles , Judge whether the number of parameters is correct and Enhance the robustness of scripts ).
1.3.2.2$#


1.3.3 $*、[email protected]
1.3.3.1 Basic grammar
$* ( Function description : This variable represents all the parameters on the command line ,$* Treat all parameters as a whole )
[email protected] ( Function description : This variable also represents all the parameters on the command line , however [email protected] Treat each parameter differently )
1.3.3.2$*[email protected] Use



1.3.3$?
1.3.3.1 Basic grammar
$? ( Function description : Return status of last executed command . If the value of this variable is 0, Prove the previous A command is executed correctly ;
If the value of this variable is not 0( Which number is it , It's up to the order itself ), Then prove The last command was executed incorrectly .)
1.3.3.2 Judge parameter.sh Whether the script is executed correctly

边栏推荐
- JS regular delete the span tag and the content in the tag
- Three cache strategies: cache side strategy, read/write through strategy, and write back strategy
- What's the use of volatile
- What if Alibaba cloud international forgets its member name or login password?
- 阿里云国际版注册成功后添加支付方式
- 深度解析kube-scheduler调度上下文
- Networkx visualizes graphs
- TextView展示不完的内容实现--全显示、部分显示
- flink使用ListState实现KeyedState
- 云计算或成时代新拐点?从哪些点可以看出?
猜你喜欢
随机推荐
odbc excel--2022-07-21
Arduino中断实现上升沿检测,并执行其他函数
SSH 免密登陆配置
Flink原理初探和流批一体API(二)v2
什么是NFT?你不会还不知道吧!
押注全场景,荣耀胜算几何?
PostgreSQL database master-slave deployment master database suspended restore master database
气不过“自愿降薪”,裸辞面字节,四天三面,结局居然这样?
TensorRT的插件实战(1)
synchronized是如何实现的
哪种类型的模切机可用于纸?
三种缓存策略:Cache Aside 策略、Read/Write Through 策略、Write Back 策略
半定制数字反相器版图绘制
volatile有什么用
Mria + RLOG 新架构下的 EMQX 5.0 如何实现 1 亿 MQTT 连接
Has the live broadcast function of the multi merchant system been used? 666 for used friends!
flink使MapState实现KeyedState
来,滑动到下一个小姐姐
批量可视化目标检测标注框——YOLO格式数据集
flink使用ListState实现KeyedState









