当前位置:网站首页>Shell基础入门
Shell基础入门
2022-06-30 12:12:00 【向上的狼】
一、Shell变量
1.1、定义变量时, 变量名不加美元符号
- 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头。
- 中间不能有空格,可以使用下划线(_)。
- 不能使用标点符号。
- 不能使用bash里的关键字(可用help命令查看保留关键字)
1.2、变量的类型
局部变量
- 局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量。
环境变量
- 所有的程序,包括shell启动的程序,都能访问环境变量,有些程序需要环境变量来保证其正常运行
Shell变量
- shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量,有一部分是局部变量
# 变量的声明name="zhangsan"# 变量的调用echo $nameecho ${name}# 只读变量 /bin/sh: NAME: This variable is read only.url="https://www.google.com"readonly urlurl="https://www.runoob.com"# 删除变量unset name
二、Shell的字符串
- 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
- 单引号字串中不能出现单独一个的单引号,但可成对出现,作为字符串拼接使用。
双引号:
- 双引号里可以有变量
- 双引号里可以出现转义字符
# 声明字符串str1="hello world 1"str2='hello world 2'str3='hello world3# 字符串拼接--双引号name='sunwukong'name1="hello, "$name"" //可以拼接name2="hello, ${name}" //可以拼接# 字符串拼接--单引号passwd='123456'passwd1='hello, '$passwd'' //可以拼接passwd2='hello, ${passwd}' //不能拼接echo $passwd2 # hello, ${passwd} !# 字符串的长度echo ${#email} //获取字符串的长度echo ${email:1:4} //获取email字符串从1开始, 取4个字符(字符串的计数是从开始的)
三、Shell数组
- bash支持一维数组(不支持多维数组),并且没有限定数组的大小。
- 数组元素的下标由 0 开始编号。获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于 0。
# 定义数组 括号来表示数组,数组元素用"空格"符号分割开数组名=(值1 值2 ... 值n)favs=("足球" "蓝球" "乒乓球球" "保龄球")# 读取数组 ${ 数组名[下标]}fav=${favs[1]}# 使用 @ 符号可以获取数组中的所有元素echo ${favs[@]}# 获取数组的长度length1=${#favs[@]}length2=${#favs[*]}
四、Shell的注释
- 以 # 开头的行就是注释,会被解释器忽略。
- 通过每一行加一个 # 号设置多行注释
#--------------------------------------------# 这是一个注释# author:# site:#--------------------------------------------##### 服务器配置-start ############### 服务器配置-end ###### 特殊的多行注释:<<EOF注释内容...注释内容...注释内容...EOF:<<!注释内容...注释内容...注释内容...!注: 这里EOF和!都是可以更改的, 但是得保证前面和后面都得是一样的
五、Shell参数传递
执行Shell脚本时, 向脚本传递参数, 脚本内获取参数的格式为: $n, n代表一个数字.

#!/bin/bashecho "Shell 传递参数实例!";echo "执行的文件名:$0";echo "第一个参数为:$1";echo "第二个参数为:$2";echo "第三个参数为:$3";# ./hello.sh 11 22 33 44
边栏推荐
猜你喜欢
A review of quantum neural networks 2022 for generating learning tasks
Google refutes rumors and gives up tensorflow. It's still alive!
Use of polarplot function in MATLAB
Four Misunderstandings of Internet Marketing
Iserver publishing es service query setting maximum return quantity
Linux系统Redis的安装
海思3559 sample解析:venc
[cloud native | kubernetes] in depth understanding of deployment (VIII)
市值蒸发650亿后,“口罩大王”稳健医疗,盯上了安全套
iServer发布ES服务查询设置最大返回数量
随机推荐
How to detect 3D line spectral confocal sensors in semiconductors
Hisilicon 3559 sample parsing: Venc
QT MSVC installation and commissioning
Construction de la plate - forme universelle haisi 3559: obtenir le codage après modification du cadre de données
NoSQL - redis configuration and optimization
How to use the plug-in mechanism to gracefully encapsulate your request hook
Building of Hisilicon 3559 universal platform: obtaining the modified code of data frame
Introduction to sub source code updating: mid May: uniques NFT module and nomination pool
Some commonly used hardware information of the server (constantly updated)
Substrate 源码追新导读: Call调用索引化, 存储层事物化全面完成
90. (cesium chapter) cesium high level listening events
海思3559 sample解析:venc
Tencent cloud Database Engineer competency certification was launched, and people from all walks of life talked about talent training problems
Sublist3r error reporting solution
Layout of pyqt5 interface and loading of resource files
Today in history: Microsoft acquires PowerPoint developers; SGI and MIPS merge
海思3559萬能平臺搭建:獲取數據幀修改後編碼
品达通用权限系统(Day 7~Day 8)
Achieve secure data sharing among multiple parties and solve the problem of asymmetric information in Inclusive Finance
Remove invalid parentheses [simulate stack with array]