当前位置:网站首页>shell 字符串变量
shell 字符串变量
2022-06-25 13:32:00 【响彻天堂丶】
1 介绍
字符串(String)就是一系列字符的组合。字符串是 Shell 编程中最常用的数据类型之一(除了数字和字符串,也没有其他类型了)
2 字符串的三种格式
1 单引号方式
2 双引号方式, 推荐
3 不用引号方式
vim string.sh
#!/bin/bash
name1='james'
name2="kobe"
name3=paul
echo "name1: ${name1}"
echo "name2: ${name2}"
echo "name3: ${name3}"

3 字符串的三种格式区别
1 使用单引号’'的字符串
任何字符都会原样输出,在拼接字符串中使用变量是无效的。
2 由双引号" "包围的字符串
其中包含了变量,那么该变量会被解析得到值,而不是原样输出。
3 不被引号包围的字符串
不被引号包围的字符串中出现变量时也会被解析,这一点和双引号" "包围的字符串一样。
字符串中不能出现空格,否则空格后边的字符串会作为其他命令解析。
demo:
vim string.sh
#!/bin/bash
name="kobe"
echo '使用单引号打印 name: ${name}'
echo "使用双引号打印 name: ${name}"
echo 不适用引号打印name:${
name}

4 获取字符串长度
4.1 语法
${#字符串变量名}
4.2 含义
获取字符串长度
#!/bin/bash
name="kobe"
echo '使用单引号打印 name: ${name}'
echo "使用双引号打印 name: ${name}"
echo 不适用引号打印name:${
name}
echo "字符串长度为: ${#name}"
4.3 结果

5 字符串拼接
5.1 拼接方式
1 无符号拼接
2 双引号拼接
5.2 demo
#!/bin/bash
hello="hello"
world="world"
hello_world=${
hello}${
world}
echo "无符号拼接: ${hello_world}"
hello_world1="${hello}${world}"
echo "双引号拼接:${hello_world}"

6 字符串截取
6.1 语法
| 格式 | 说明 |
|---|---|
${变量名:start:length} | 从 string 字符串的左边第 start 个字符开始, 向右截取 length 个字符。start从0开始 |
${变量名:start} | 从 string 字符串的左边第 start 个字符开始截取,直到最后。 |
${变量名:0-start:length} | 从 string 字符串的右边第 start 个字符开始, 向右截取 length 个字符。start从1开始, 代表右侧第一个字符 |
${变量名:0-start} | 从 string 字符串的右边第 start 个字符开始截取,直到最后。 |
${变量名#*chars} | 从 string 字符串左边第一次出现 *chars 的位置开始, 截取 *chars 右边的所有字符。 |
${变量名##*chars} | 从 string 字符串左边最后一次出现 *chars 的位置开始, 截取 *chars 右边的所有字符。 |
${变量名%chars*} | 从 string 字符串右边第一次出现 chars* 的位置开始, 截取 chars* 左边的所有字符。 |
${变量名%%chars*} | 从 string 字符串右边最后一次出现 chars* 的位置开始, 截取 chars* 左边的所有字符 |
6.2 demo
字符串"hello world"
从左侧第0个开始,向左截取2个字符
从左侧第5个开始,向左截取所有字符
从右侧第5个开始,向右截取2个字符
截取左边第一次出现字符l右边的所有字符
截取左边最后一次出现字符l右边的所有字符
截取右边第一次次出现字符l左边的所有字符
截取右边最后一次出现字符l左边的所有字符
#!/bin/bash
string="hello world"
echo "字符串为: ${string}"
str1="${string:0:2}"
echo "从左侧第0个开始,向左截取2个字符: ${str1}"
str2="${string:5}"
echo "从左侧第5个开始,向左截取所有字符: ${str2}"
str3="${string:0-5:2}"
echo "从右侧第5个开始,向右截取2个字符: ${str3}"
str4="${string#*l}"
echo "截取左边第一次出现字符l右边的所有字符: ${str4}"
str5="${string##*l}"
echo "截取左边最后一次出现字符l右边的所有字符: ${str5}"
str6="${string%l*}"
echo "截取右边第一次次出现字符l左边的所有字符: ${str6}"
str7="${string%%l*}"
echo "截取右边最后一次出现字符l左边的所有字符: ${str7}"

边栏推荐
- go---- mgo
- How to determine if a web worker has been created or closed
- sigmoid函数sigmoid求导
- Hash table, hash conflict
- [open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release
- Go--- route filter
- Table de hachage, conflit de hachage
- 哈希錶、哈希沖突
- Discuz仿今日头条模板/Discuz新闻资讯商业版GBK模板
- 如何在 2022 年为 Web 应用程序选择技术堆栈
猜你喜欢

网络远程访问的方式使用树莓派
Deep parsing and implementation of redis stream advanced message queue [10000 words]

leetcode:剑指 Offer II 091. 粉刷房子【二维dp】

VGA display of de2-115 FPGA development board

历史上的今天:网易成立;首届消费电子展召开;世界上第一次网络直播

论文阅读:Graph Contrastive Learning with Augmentations

Nr-arfcn and channel grid, synchronous grid and GSCN
![Leetcode: Sword finger offer II 091 Painting house [2D DP]](/img/d7/dc8a3522dbd58b4573cfd75497460c.png)
Leetcode: Sword finger offer II 091 Painting house [2D DP]

On the realization of guessing numbers game

Realization of neural networks with numpy
随机推荐
How unity makes the UI intercept click events
多台云服务器的 Kubernetes 集群搭建
關於一道教材題的講解
sigmoid函数sigmoid求导
Simple realization of mine sweeping
分类器与cross entropy loss函数
VGA display of de2-115 FPGA development board
Logistic Regression VS Linear Regression
go---- mgo
Cesium model Daquan glb/glft format
对白:推荐系统快速入门路线及各知识点总结
Parabolic motion in unity 2D games
The starting point for learning programming.
Explication d'un problème de manuel
Cesium learning notes
leetcode:456. 132 模式【单调栈】
Error1822 and error1824 are displayed in the database
Is it safe for Guosen Securities to open a stock account? Excuse me?
Nova中的api
Application of tactile intelligent sharing-rk3568 in financial self-service terminal