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

边栏推荐
- Shell变量、系统预定义变量$HOME、$PWD、$SHELL、$USER、自定义变量、特殊变量$n、$#、$*、[email protected]、$?、env看所有的全局变量值、set看所有变量
- LeetCode.1260. 二维网格迁移____原地暴力 / 降维+循环数组直接定位
- Voice live broadcast system - Principles to be followed in developing push notifications
- ACL2021最佳论文出炉,来自字节跳动
- NFT系统开发-教程
- Talk about 10 scenarios of index failure. It's too stupid
- 刷题《剑指Offer》day03
- Food safety | the more you eat junk food, the more you want to eat it? Please keep this common food calorimeter
- 吃透Chisel语言.23.Chisel时序电路(三)——Chisel移位寄存器(Shift Register)详解
- NPM common commands
猜你喜欢

Acl2021 best paper released, from ByteDance

When I went to oppo for an interview, I got numb

Redis 为什么这么快?Redis 的线程模型与 Redis 多线程

刷题《剑指Offer》day04

Overview of PCL modules (1.6)
![Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号](/img/31/ed0d8c1a5327059f2de7493bec1c6c.png)
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号

MOS drive in motor controller

吃透Chisel语言.27.Chisel进阶之有限状态机(一)——基本有限状态机(Moore机)

吃透Chisel语言.23.Chisel时序电路(三)——Chisel移位寄存器(Shift Register)详解

圆环工件毛刺(凸起)缺口(凹陷)检测案例
随机推荐
What happens if the MySQL disk is full? I really met you!
Acl2021 best paper released, from ByteDance
并发之park与unpark说明
Oracle RAC 19C PDB instance is down
C # set different text watermarks for each page of word
Expose a technology boss from a poor family
7/26 思维+dp+后缀数组的学习
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
[scm] source code management - lock of perforce branch
吃透Chisel语言.27.Chisel进阶之有限状态机(一)——基本有限状态机(Moore机)
视觉SLAM十四讲笔记(一):第一讲+第二讲
Anchor Free检测器:CenterNet
NPM common commands
GBase 8a MPP集群扩容实战
Visual slam lecture notes (I): Lecture 1 + Lecture 2
华为交换机双上行组网Smart-link配置指南
2016展望
并发之线程状态转换
【云原生 • DevOps】一文掌握容器管理工具 Rancher
35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction