技术标签: linux bash 字符串 连接
在本小节中,将学习如何在Bash Shell脚本中添加或连接字符串。
在bash脚本编制中,可以将两个或多个字符串添加或连接在一起,这称为字符串连接。它是任何一种编程语言的通用要求之一。应用特殊字符或内置函数来执行字符串连接。但是,Bash不包含任何内置函数来组合字符串数据或变量。在bash中执行字符串连接的最简单方法是并排写入变量。
例如,假设有两个字符串(即"Welcome"和"to Yiibai"),要将这两个字符串连接在一起,然后创建了一个新字符串("Welcome to Yiibai"),这种概念称为字符串连接。
语法命令
用于连接字符串的命令定义为:
str3="$str1$str2"
注意:遵守上面的命令; 赋值(=)运算符之前或之后不应有任何空格。str用于指示字符串。
此命令将串联str1和str2变量的值,并将其存储在第三个变量str3中。
以下是一些说明了字符串连接的不同方式的示例:
示例1:并排写入变量连接
这是字符串连接的基本示例,并且在此方法中不需要其他运算符或函数。
Bash脚本
#!/bin/bash
#Script to Concatenate Strings
#Declaring the first String
str1="We welcome you"
#Declaring the Second String
str2=" on Yiibai."
#Combining first and second string
str3="$str1$str2"
#Printing a new string by combining both
echo $str3
执行上面示例代码,得到以下结果:
示例2:使用双引号连接
另一个方法是在字符串中使用双引号定义的变量。字符串变量可以应用于字符串数据的任何位置。
Bash脚本
#!/bin/bash
#Script to Concatenate Strings
#Declaring String Variable
str="We welcome you"
#Add the variable within the string
echo "$str on Yiibai."
执行上面示例代码,得到以下结果:
[email protected]:~/bashcode$ cat /dev/null > concat-string.sh
[email protected]:~/bashcode$ vi concat-string.sh
[email protected]:~/bashcode$ ./concat-string.sh
We welcome you on Yiibai.
示例3:将追加运算符与循环一起使用连接
大多数流行的编程语言都支持追加运算符(+=),它是加号和等号的组合。它将新的字符串添加到字符串变量的末尾。
Bash脚本
#!/bin/bash
echo "Printing the name of the programming languages"
#Initializing the variable before combining
lang=""
#for loop for reading the list
for value in 'java' 'python' 'C' 'C++' 'Bash';
do
lang+="$value " #Combining the list values using append operator
done
#Printing the combined values
echo "$lang"
执行上面示例代码,得到以下结果:
[email protected]:~/bashcode$ cat /dev/null > concat-string.sh
[email protected]:~/bashcode$ vi concat-string.sh
[email protected]:~/bashcode$ ./concat-string.sh
Printing the name of the programming languages
java python C C++ Bash
示例4:使用Printf函数连接
在bash中,printf是用于打印和连接字符串的函数。
Bash脚本
#!/bin/bash
str="Welcome"
printf -v new_str "$str to Yiibai."
echo $new_str
执行上面示例代码,得到以下结果:
[email protected]:~/bashcode$ cat /dev/null > concat-string.sh
[email protected]:~/bashcode$ vi concat-string.sh
[email protected]:~/bashcode$ ./concat-string.sh
Welcome to Yiibai.
示例5:使用文字字符串连接
字符串连接也可以通过大括号{}与文字字符串一起执行,使用应避免变量与文字字符串混淆。
Bash脚本
#!/bin/bash
str="Welcome to"
newstr="${str} Yiibai."
echo "$newstr"
执行上面示例代码,得到以下结果:
[email protected]:~/bashcode$ cat /dev/null > concat-string.sh
[email protected]:~/bashcode$ vi concat-string.sh
maxs[email protected]:~/bashcode$ ./concat-string.sh
Welcome to Yiibai.
示例6:使用下划线连接
使用下划线在bash shell中连接字符串是常见的任务之一,它主要用于为文件分配名称。
Bash脚本
#!/bin/bash
str1="Hello"
str2="World!"
echo "${str1}_${str2}"
执行上面示例代码,得到以下结果:
[email protected]:~/bashcode$ cat /dev/null > concat-string.sh
[email protected]:~/bashcode$ vi concat-string.sh
[email protected]:~/bashcode$ ./concat-string.sh
Hello_World!
示例7:使用任意字符连接
Bash脚本
#!/bin/bash
#String Concatenation by Character (,) with User Input
read -p "Enter First Name: " name
read -p "Enter City: " state
read -p "Enter Age: " age
combine="$name,$state,$age"
echo "Name, City, Age: $combine"
执行上面示例代码,得到以下结果:
字符串连接是编程语言中生成有意义的输出所必需的功能之一。本小节中介绍了在bash中连接字符串的几种常见的方法。
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。
lua中for的四种遍历方式区别table.maxn 取最大的整数key#table 从1开始的顺序整数最大值,如1,2,3,6 #table == 3key,valuepairs 取每一个键值对ipairs 取从key==1开始的顺序整数最大值,每个键值对参考http://rangercyh.blog.51cto.com/1444712/1032925不过...
Idea2019搭建Mybatis框架环境并完成一个简单的添加功能使用Idea搭建Mybatis环境1、通过Idea创建一个新的项目:选择Maven,小框框勾选,直接选择下一步输入自定义包名和项目名。2、项目创建成功后,在左边的目录结构中选择pom.xml(一般项目创建后会自动打开),在xml文件中引入mybatis的jar和mysql的文件(在一开始添加时会报错,在idea右下边弹出的提...
Bootstrap之栅格布局最近在学习基于boostrap下的栅格布局,这个布局功能非常强大,提供了一套响应式的布局解决方案,可实现在PC端,平板端和移动端切换。下面是一些总结:1.栅格盒模型的精妙之处容器两边具有15px的padding;行:两边具有-15px的margin;列:两边具有15px的padding;为了维护槽宽规则,列两边必须要有15px的padding,为了能使列嵌套...
PEP 8: E402 module level import not at top of file
之前运行程序的时候无意间遇到了一个错误,FTH: (3460): Fault tolerant heap shim applied to current process. This is usually due to previous crashes.产生的背景:实际上,之前对一个程序进行测试的时候,测着测着就冒出来了这个错误,具体是什么原因导致的,我也不知道;根据提示的错误推测,好像是由于之前的崩溃而导致的;后来搜索了一圈也没找到,再后来在高人的指点下解决了,现在提供一种可能解决的方法(至少我是解决了
报错:words = lcut(passage) #精确模式分词形式NameError: name 'lcut' is not definedwords = lcut(passage)改为words = jieba.lcut(passage)也不行,继续报错如下图:words = jieba.lcut(passage) #精确模式分词形式AttributeError: module 'jieba' has no attribute 'lcut'原因查找:明明安装了jieba,也导入了fr
1、打开注册表,找到HKEY_LOCAL_MACHINE/software/javasoft,将其删除。2、将系统盘中Windows\System32和Windows\SysWOW64文件夹下的java.exe, javaw.exe 和 javaws.exe文件删掉,若某个文件不存在,则不用管。
通过直方图的方法 每张图片都可以生成其灰度图像直方图(histogram)。如果两张图片的直方图很接近,就可以认为它们很相似。 因此,此处我们利用两幅图像的直方图来进行相似度的比较。原理较为简单,具体算法如下:1、获得输入灰度图像的直方图分布;2、将直方图划分为64个区,每个区为连续的4个灰度等级;3、对每个区的4个值进行求和运算,得到1个数据,如此,会
use DATABASE_NAME 创建数据库数据库不存在就创建数据库,存在则切换到数据库db 查看当前数据库show dbs 列出所有的数据库刚创建的数据库要插入数据才会显示在列表中db.Collection_Name.insert({"key":"value"})在插入数据的时候若没有相应的集合也会自动创建。db.dropDatabase() 删除当前的数据库要删除指定...
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家!目录一丶注册和登录以及用户退出功能二丶上传头像功能和修改用户名功能测试三丶发布房源以及实名认证功能测试四丶网站房屋搜索功能测试五丶我的订单功能以及客户订单功能测试六丶网站主页房屋幻灯片功能测试一丶注册和登录以及用户退出功能1.注册功能step1 使用博主自己的手机号码进行注册功能测试,注册界...
系统应该是Alpine Linux LXD(Linux容器),那么入股在其中安装 bash shell呢apk updateapk upgradeapk add bash# 安装 bash 文档apk add bash-doc# 安装 bash 自动命令补全apk add bash-completion# 使用 bash 作为 shellbash# 需要从主机登录到 Alpine Linux LXD 虚拟机,比如我从kubernets进去kubectl exec -it .