当前位置:网站首页>Shell脚本-字符串
Shell脚本-字符串
2022-07-01 08:36:00 【小蜗牛的路】
字符串(String)就是一系列字符的组合。字符串是 Shell 编程中最常用的数据类型之一(除了数字和字符串,也没有其他类型了)。
字符串可以由单引号' '包围,也可以由双引号" "包围,也可以不用引号。
字符串举例
str1=jack
str2="rose"
str3='100'
三种形式的区别
- 由单引号
' '包围的字符串:
- 任何字符都会原样输出,在其中使用变量是无效的。
- 字符串中不能出现单引号,即使对单引号进行转义也不行。
- 由双引号
" "包围的字符串:
- 如果其中包含了某个变量,那么该变量会被解析(得到该变量的值),而不是原样输出。
- 字符串中可以出现双引号,只要它被转义了就行。
- 不被引号包围的字符串
- 不被引号包围的字符串中出现变量时也会被解析,这一点和双引号
" "包围的字符串一样。 - 字符串中不能出现空格,否则空格后边的字符串会作为其他变量或者命令解析。
脚本
#!/bin/bash
name=jack
str1=我的名字是$name
str2="有个人的\"名字\"叫$name"
str3='我朋友叫 $name'
echo $str1
echo $str2
echo $str3
输出:
我的名字是jack
有个人的"名字"叫jack
我朋友叫 $name
str1 中包含了$name,它被解析为变量 name 的引用。
str2 中包含了引号,但是被转义了(由反斜杠\开头的表示转义字符)。str2 中也包含了$name,它也被解析为变量 name 的引用。
str3 中也包含了$name,但是仅仅是作为普通字符,并没有解析为变量 name 的引用。
获取字符串长度
具体方法如下,${#string_name},string_name 表示字符串名字。
代码如下:
#!/bin/bash
name=jack
echo ${#name}
输出:
4
边栏推荐
- View drawing process analysis
- V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
- Model and view of QT
- MATLAB【函数求导】
- DataBinding源码分析
- Intelligent water supply system solution
- 你了解数据是如何存储的吗?(C整型和浮点型两类)
- Redis publish subscription
- win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
- Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
猜你喜欢

Nacos - 配置管理

Computer tips

vscode自定义各个区域的颜色

Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates

"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis

Field agricultural irrigation system
![Matlab [function derivation]](/img/ba/9fb9da8a458d0c74b29b21a17328fc.png)
Matlab [function derivation]

Intelligent water and fertilizer integrated control system

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

Screenshot tips
随机推荐
Huawei machine test questions column subscription Guide
Review of week 280 of leetcode
[MFC development (17)] advanced list control list control
I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
Glitch free clock switching technology
vscode自定义各个区域的颜色
AES简单介绍
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
Shell脚本-select in循环
长安链同步节点配置与启动
Matlab tips (23) matrix analysis -- simulated annealing
In depth learning training sample amplification and tag name modification
中考体育项目满分标准(深圳、安徽、湖北)
MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
【面试必刷101】链表
Intelligent water conservancy solution
C语言指针的进阶(下)
yolov5训练可视化指标的含义
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
如何做好固定资产管理?易点易动提供智能化方案