当前位置:网站首页>Shell脚本-变量的定义、赋值和删除
Shell脚本-变量的定义、赋值和删除
2022-07-01 08:36:00 【小蜗牛的路】
传统:什么是Shell
- Shell 是一个应用程序,它连接了用户和 Linux 内核,让用户能够更加高效、安全、低成本地使用 Linux 内核,这就是 Shell 的本质。
- Shell 也是一种编程语言,它的编译器(解释器)是 Shell 这个程序。
- Shell 是一种脚本语言,我们编写完源码后不用编译,直接运行源码即可。
Shell脚本格式
以#!/bin/bash开头
#!/bin/bash
···
Shell变量:Shell变量的定义、赋值和删除
推荐值value用引号包围
variable='value'
variable="value"
单引号和双引号的区别
- 以单引号’ '包围变量的值时,单引号里面是什么就输出什么
- 以双引号" "包围变量的值时,输出时会先解析里面的变量和命令,而不是把双引号中的变量名和命令原样输出
#!/bin/bash
author='jack'
name='作家的名字是${author}'
name2="作家的名字是${author}"
echo ${name}
echo ${name2}
运行脚本输出:
作家的名字是${author}
作家的名字是jack
将命令的执行结果赋值给变量
支持将命令的执行结果赋值给变量,把命令包围起来variable=$(command)
#!/bin/bash
out=$(sh test.sh)
echo ${out}
输出
作家的名字是${author} 作家的名字是jack
只读变量
使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变。
下面的例子尝试更改只读变量,结果报错:
#!/bin/bash
name='jack'
readonly name
name='rose'
echo ${name}
输出
test3.sh: line 5: name: readonly variable
删除变量
使用 unset 命令可以删除变量。语法unset variable_name
变量被删除后不能再次使用;unset 命令不能删除只读变量。
#!/bin/bash
name='jack'
unset name
echo ${name}
输出
边栏推荐
- Screenshot tips
- 【js逆向】md5加密参数破解
- Audio-AudioRecord create(一)
- Mavros sends a custom topic message to Px4
- Intelligent constant pressure irrigation system
- Foundation: 2 The essence of image
- DID的使用指南,原理
- There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way
- MD文档中插入数学公式,Typora中插入数学公式
- C语言学生信息管理系统
猜你喜欢

Matlab tips (23) matrix analysis -- simulated annealing

How can enterprises and developers take the lead in the outbreak of cloud native landing?

Li Kou 1358 -- number of substrings containing all three characters (double pointer)

中小企业固定资产管理办法哪种好?

截图小妙招

Internet of things technology is widely used to promote intelligent water automation management

Nacos - 配置管理
![[untitled]](/img/40/560c597d53d27eff860644cd7c3030.png)
[untitled]

What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?

Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
随机推荐
Interrupt sharing variables with other functions and protection of critical resources
嵌入式工程师面试-常问问题集
Shell脚本-for循环和for int循环
[deep analysis of C language] - data storage in memory
Field agricultural irrigation system
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
《单片机原理及应用》-片外拓展
TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works
MATLAB【函数和图像】
5mo3 UHI HII HII 17mn4 19Mn6 executive standard
Guidelines and principles of did
内存大小端
如何做好固定资产管理?易点易动提供智能化方案
View drawing process analysis
It is designed with high bandwidth, which is almost processed into an open circuit?
FreeRTOS learning easy notes
Li Kou 1358 -- number of substrings containing all three characters (double pointer)
SPL Introduction (I)