当前位置:网站首页>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

边栏推荐
猜你喜欢

“蔚来杯“2022牛客暑期多校训练营1

Promise (II)

气不过“自愿降薪”,裸辞面字节,四天三面,结局居然这样?

为什么有的人把代码写的如此复杂?

Redis中的数据类型

园区/厂区怎么实现wifi上网短信认证

Come on, slide to the next little sister

C language decimal number to binary number

Redistemplate pipeline use

PostgreSQL database master-slave deployment master database suspended restore master database
随机推荐
原生js实现dom的获取及操作
Redis 持久化操作 (RDB, AOF)
Mria + RLOG 新架构下的 EMQX 5.0 如何实现 1 亿 MQTT 连接
三种缓存策略:Cache Aside 策略、Read/Write Through 策略、Write Back 策略
y74.第四章 Prometheus大厂监控体系及实战 -- PromQL简介和监控pod资源(五)
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
volatile有什么用
js 正则删除span标签以及标签里面的内容
What level of futures company is founder metaphase? Is the account opening safe and reliable?
张宇高数30讲总结
oh-my-zsh
Flink高级API(三)
Come on, slide to the next little sister
Algorithm --- use the minimum cost to climb stairs (kotlin)
How to realize synchronized
Several ways of QT thread exit
Object prototype of typescript object extension__ proto__ And prototype
气不过“自愿降薪”,裸辞面字节,四天三面,结局居然这样?
Typora set the title to automatically add sequence number
Reading notes - > statistics] construction of 12-02 confidence interval -t distribution concept introduction