博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux bash 字符串 连接,Bash连接字符串
阅读量:7000 次
发布时间:2019-06-27

本文共 3063 字,大约阅读时间需要 10 分钟。

在本小节中,将学习如何在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

执行上面示例代码,得到以下结果:

48a9f15b7708854159acda8102ca2f36.png

示例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."

执行上面示例代码,得到以下结果:

maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh

maxsu@yiibai:~/bashcode$ vi concat-string.sh

maxsu@yiibai:~/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"

执行上面示例代码,得到以下结果:

maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh

maxsu@yiibai:~/bashcode$ vi concat-string.sh

maxsu@yiibai:~/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

执行上面示例代码,得到以下结果:

maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh

maxsu@yiibai:~/bashcode$ vi concat-string.sh

maxsu@yiibai:~/bashcode$ ./concat-string.sh

Welcome to Yiibai.

示例5:使用文字字符串连接

字符串连接也可以通过大括号{}与文字字符串一起执行,使用应避免变量与文字字符串混淆。

Bash脚本

#!/bin/bash

str="Welcome to"

newstr="${str} Yiibai."

echo "$newstr"

执行上面示例代码,得到以下结果:

maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh

maxsu@yiibai:~/bashcode$ vi concat-string.sh

maxsu@yiibai:~/bashcode$ ./concat-string.sh

Welcome to Yiibai.

示例6:使用下划线连接

使用下划线在bash shell中连接字符串是常见的任务之一,它主要用于为文件分配名称。

Bash脚本

#!/bin/bash

str1="Hello"

str2="World!"

echo "${str1}_${str2}"

执行上面示例代码,得到以下结果:

maxsu@yiibai:~/bashcode$ cat /dev/null > concat-string.sh

maxsu@yiibai:~/bashcode$ vi concat-string.sh

maxsu@yiibai:~/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"

执行上面示例代码,得到以下结果:

f3880d92e0e377a290a6535b640721f0.png

字符串连接是编程语言中生成有意义的输出所必需的功能之一。本小节中介绍了在bash中连接字符串的几种常见的方法。

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

转载地址:http://scevl.baihongyu.com/

你可能感兴趣的文章
封装CIImage实现实时渲染
查看>>
log4delphi使用(转)
查看>>
5月23日Google就宣布了Chrome 36 beta
查看>>
设计模式---工厂模式Factory(创建型)
查看>>
C++ Primer 学习笔记_91_用于大型程序的工具 --命名空间
查看>>
window.print实现打印特定控件或内容
查看>>
前端必读:浏览器内部工作原理
查看>>
Ubuntu下deb包的安装方法
查看>>
微软职位内部推荐-SDE II
查看>>
golang的interface到其他类型的数据转换
查看>>
jquery-pager分页
查看>>
【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】
查看>>
跨终端 Web
查看>>
【BZOJ】1503: [NOI2004]郁闷的出纳员(Splay)
查看>>
C++标准库之 Lower_Bound, upper_Bound
查看>>
ASP.NET编辑与更新数据(非GridView控件实现)
查看>>
spark scala学习笔记
查看>>
NeHe OpenGL教程 第三课:颜色渲染
查看>>
qml demo分析(photosurface-图片涅拉)
查看>>
BZOJ 2463: [中山市选2009]谁能赢呢?[智慧]
查看>>